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

Regression using ngOptions asynchronously in angular 1.3 #9714

Closed

Description

@FrenchDilettante

Hi guys,

Just wanted to start by saying that Angular is a wonderful project and I'm having a great time working with it :)

I have an example of the issue in this jsFiddle: http://jsfiddle.net/88aw4yyq/

Consider this HTML:

<select name="test"
        ng-model="test"
        ng-options="opt.id as opt.label for opt in availableOptions">
</select>

And this controller:

$scope.test = '';
$scope.availableOptions = [];

$timeout(function () {
    $scope.availableOptions = [
        {id: '', label: 'Please select...'},
        {id: 1, label: 'opt1'},
        {id: 2, label: 'opt2'}
    ];
}, 1000);

The $timeout simulates any regular AJAX request. {id: '', label: 'Please select...'} is added for convenience for the user.

Expected behavior: 'Please select...' should be selected once the data is loaded.

Actual behavior: An empty option is added on top in the select field.

Angular versions: 1.3.0 this is a regression, 1.2.x was working fine.

Browser versions: Tested on Fx/Chrome (latest versions) on both OS X & Windows.

If anyone else is encountering the issue, here is a workaround:

$scope.test = '';
$scope.availableOptions = [
    {id: '', label: 'Please select...'}
];

$timeout(function () {
    $scope.availableOptions = $scope.availableOptions.concat([
        {id: 1, label: 'opt1'},
        {id: 2, label: 'opt2'}
    ]);
}, 1000);

Activity

  1. anotherchrisberry commented on Oct 29, 2014

    @anotherchrisberry

    I just ran into this same issue (plunker here: http://plnkr.co/edit/NgPGrklA3ng6bmnPjxmw?p=preview) and ended up with the same solution (pre-filling the options holder with the known selected value). The workaround is kind of ugly but not terrible.

    Tested in Firefox, Chrome and Safari but did not see the behavior in Firefox. Only tested on OS X.

  2. added this to the milestone on Oct 30, 2014
  3. ajohnston256 commented on Nov 22, 2014

    @ajohnston256

    Can you assign this to me? I think I see the problem, and I'll try to get a pull request ready for tomorrow.

  4. added 3 commits that reference this issue on Nov 23, 2014
    c03d6b0
    4459d38
    62f242c
  5. modified the milestones: 1.3.5, on Nov 23, 2014
  6. self-assigned this
    on Nov 25, 2014
  7. added a commit that references this issue on Nov 26, 2014
    8e2aa27
  8. 39 remaining items

  9. modified the milestones: , 1.3.8 on Dec 19, 2014
  10. angrytoro commented on Dec 29, 2014

    @angrytoro

    maybe it cause by the code

    self.addOption = function(value, element) {
        assertNotHasOwnProperty(value, '"option value"');
        optionsMap[value] = true;
    
        if (ngModelCtrl.$viewValue == value) {
          $element.val(value);
          if (unknownOption.parent()) unknownOption.remove();
        }
        // Workaround for https://code.google.com/p/chromium/issues/detail?id=381459
        // Adding an <option selected="selected"> element to a <select required="required"> should
        // automatically select the new element
        if (element && element[0].hasAttribute('selected')) {
          element[0].selected = true;
        }
    };
    

    and the code make the select don't show the real value.

    if (ngModelCtrl.$viewValue == value) {
          $element.val(value);
          if (unknownOption.parent()) unknownOption.remove();
    }
    
  11. added a commit that references this issue on Jan 4, 2015
    4846404
  12. petebacondarwin commented on Jan 4, 2015

    @petebacondarwin
    Contributor

    @angrytoro - this is the right idea but ngOptions needs a full refactoring, I have provided a PR in #10639...

  13. added 9 commits that reference this issue on Jan 4, 2015
    149f7d9
    2a0c551
    0d9a246
    089bc59
    db330a1
    e9e1fd0
    263dd22
    7f3b231
    d43d943
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions