如何使第一个选项 selected in selected select

How to make first option selected in chosen select

我选择了select,我的选项通过ng-model和ng-options获得。如何在页面加载时设置第一个选项 selected?

<select chosen data-placeholder="Choose a project..."
    ng-model="workflow.ProjectID" 
    ng-model-options="{ updateOn: 'blur' }" 
    ng-change="addActionButtons()"
    ng-options="item.Id as item.Name for item in projectList">
    <option value=""></option>
</select>

这是控制器:

$scope.bindModel = function(data) {
    $scope.model = data;
    $scope.projectList = Global.projectList;
    $scope.Task.push($scope.newTask());
};

$scope.Task = [];

$scope.newTask = function() {
    return {                        
        ProjectId: $scope.workflow.ProjectID = $scope.projectList[0].Id,
        Date: getDate(),
        TaskDescription: '',
        TimeWorked: '1',
        Note: '',
        isSaved: false
   };

};

在你的控制器上,你必须放这样的东西:

//If workflow is never use in your controller, you have to declare it before
$scope.workflow = {}; 
$scope.workflow.ProjectID = $scope.projectList[0].Id;

您可以使用ng-init

使用

ng-init="workflow.ProjectID = projectList[0]['Id']"

您可以尝试这样的操作:

 //init list select
$scope.values = [{ id: 1, label: '1' }, { id: 2, label: '2' }, { id: 3, label: '3' }, { id: 10, label: '10' }, { id: 20, label: '20' }];
// in your html ng-init select the first items in values 
<select ng-options="item.label for item in values track by item.id" ng-model="selected"  ng-change="changeNumPerPage(selected)" ng-init="selected = values[0]"></select>