All this miss use of ng-init
.
From the angular docs:
This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting. Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope.
Source docs
In my opinion the correct way to set a default value is to simply pre-fill your ng-model
property with the value selected from your ng-options
, angular does the rest.
Essentially when you define the $scope
property your select will bind to assign it the default value from your data
array. If your data
array is from an ajax request, just assign it once you have the data
.
.controller('test', ['$scope', function ($scope) {
$scope.data = [{name: 'one', id: 1}, {name: 'two', id: 2},{name: 'three', id: 3}];
$scope.repeatSelect= $scope.data[0];
}]);
There is one caveat to note. If you employ the as
key word in your expression you have to assign your ng-model
with the actual property your telling it to select.
See full fiddle demoing both: http://jsfiddle.net/kb99gee8/