HUSTLIN
HUSTLIN

Reputation: 197

How to make first option selected in chosen select

I have chosen select, and my options gets by ng-model and ng-options. How I can make 1-st option selected when page is loaded?

<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>

This is controller:

$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
   };

};

Upvotes: 0

Views: 2662

Answers (3)

JonathanTheBrosh
JonathanTheBrosh

Reputation: 208

You can try something like this :

 //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>

Upvotes: 0

Pierre-Alexandre Moller
Pierre-Alexandre Moller

Reputation: 2354

On your controller, you have to put something like this :

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

Upvotes: 0

sahil gupta
sahil gupta

Reputation: 2349

You can use ng-init

Use

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

Upvotes: 2

Related Questions