Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Predefined model value and ng-options later binding #11727

Closed
osmirnov opened this issue Apr 25, 2015 · 3 comments
Closed

Predefined model value and ng-options later binding #11727

osmirnov opened this issue Apr 25, 2015 · 3 comments

Comments

@osmirnov
Copy link

I have the issue with Angular.js v1.3.11 and ng-options directive. The problem is next. I have a model value I know ahead but data for binding of options comes in Ajax request later and Angular.js produces that as two selected options: with my value and first one.

Html

  <body ng-controller="MainCtrl">
      <select ng-model="selectedYear" ng-options="o.year as o.year for o in options"></select>
  </body>

Javascript

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, $timeout) {
    // Known value for preselection
    $scope.selectedYear = 2013;
    $scope.options = [];

    // Ajax call emulation
    $timeout(function() {
        $scope.options = [{year: 2012}, {year: 2013}, {year: 2014}];
    });
});

Output

<select ng-model="selectedYear" ng-options="o.year as o.year for 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>

Plunker
http://plnkr.co/edit/gFZ8Z7T5DhDPvk4G9tEU?p=preview

Stackoverflow
http://stackoverflow.com/questions/29858677/predefined-model-value-and-ng-options-later-binding/

@gkalpak
Copy link
Member

gkalpak commented Apr 26, 2015

FWIW, it works as expected on v1.4.0-rc.1.

@Narretz
Copy link
Contributor

Narretz commented Apr 27, 2015

There's an unfortunate number of bugs in ngOptions 1.3.x. Most of them have been fixed in the 1.4.x branch, and would be very difficult to fix without breaking changes in 1.3

@Narretz
Copy link
Contributor

Narretz commented Sep 13, 2015

This is the same issue as #11835

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants