angularjs - Predefined model value and ng-options later binding -
i have issue angular.js v1.3.11 , ng-options directive. problem next. have model value know ahead data binding of options comes in ajax request later , angular.js produces 2 selected options: value , first one.
html
<body ng-controller="mainctrl"> <select ng-model="selectedyear" ng-options="o.year o.year o in options"></select> </body>
javascript
var app = angular.module('plunker', []); app.controller('mainctrl', function($scope, $timeout) { $scope.selectedyear = 2013; $scope.options = []; $timeout(function() { $scope.options = [{year: 2012}, {year: 2013}, {year: 2014}]; }); });
output
<select ng-model="selectedyear" ng-options="o.year o.year o in options" class="ng-pristine ng-valid ng-touched"> <option value="0" selected="selected" label="2012">2012</option> <option value="1" selected="selected" label="2013">2013</option> <option value="2" label="2014">2014</option> </select>
if use "track by" can leave $scope.selectedyear outside $timeout callback function. shouldn't worry selected attribute. change code include "track o.year" , remove "select as" part:
<select ng-model="selectedyear" ng-options="o.year o in options track o.year"></select>
also in controller can set predefined selectedyear below object.
$scope.selectedyear = {year: 2013};
here working plnkr: http://plnkr.co/edit/umfeiwlbwbnyj9wq1fex?p=preview
Comments
Post a Comment