user2161298
user2161298

Reputation: 39

Cannot define default value for my select using ng options and ng init

i don't know why it is not working when i want to precise a default value in my select:

<select ng-init="action1 = skills[0].name"
                    ng-model="action1" ng-options="skill.name for skill in skills">
            </select>

Even with:

<select ng-init="action1 = action1"
                    ng-model="action1" ng-options="skill.name for skill in skills">
            </select>

In my controller, i automatically load an user after a server request.

$scope.user = dataFromServer;
$scope.action1 = $scope.user.action1;
$scope.skills = $scope.user.skills;

My user have an array "skills". I can choose a skill in my select and i store the item choosen in $scope.action1. But if $scope.user.action1 is already set i want to have my select with this value as default.

I want to use ng-init and ng-options. Please help ^^

Upvotes: 0

Views: 28

Answers (1)

Saad Shahd
Saad Shahd

Reputation: 963

It should be without the .name:

ng-init="action1 = skills[0]"

and the skills array should be fetched before the compilation of the ng-init directive you may use something like:

<select ng-if="skills.length" ng-init="action1 = skills[0]" notice the ng-if

Upvotes: 0

Related Questions