Skip to content

Select - when ngModel is set before options, nothing gets selected #13274

Closed

Description

@klinki

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
When ngModel is asynchronously loaded before options, nothing gets selected.

Expected behavior
It should not matter if ngModel or options is loaded first.

Minimal reproduction of the problem with instructions
http://plnkr.co/edit/BeFbEy5r5WXhot8XpIF1?p=preview

What is the motivation / use case for changing the behavior?
For asynchronous code no one can guarantee what gets loaded first. It should not matter.

Please tell us about your environment:
Fedora 25

  • Angular version: 2.2

  • Browser: [Chrome 54]

  • Language: [TypeScript]

Activity

  1. vicb commented on Dec 6, 2016

    @vicb
    Contributor

    Please add steps to reproduce with your plunker (ie what do you see vs what you expect to see)

  2. klinki commented on Dec 6, 2016

    @klinki
    Author

    Right. Sorry I forgot to publish latest changes in plunker.

    Now in example there are 2 selects:

    • for first one, options get loaded first and selected value after that
    • for second one, selected value gets loaded first and options is delayed

    I expect to see the same results for both - value being correctly selected in select.

  3. DzmitryShylovich commented on Dec 6, 2016

    @DzmitryShylovich
    Contributor

    I still not fully understand what the issues is.

    1. None of the options are actually selected http://prntscr.com/dg88vg
    2. No idea what is going on here
    let ids = data.map(item => item.id);
    let currentIds = this.countries.map(item => item.id);
    let indices = ids.map(id => currentIds.indexOf(id));
     let selection = indices.map(id => this.countries[id]);
     this.selectedCountries = selection;
    

    but I just added setTimeout and they both work identically so there's no issue with loading order.

  4. DzmitryShylovich commented on Dec 6, 2016

    @DzmitryShylovich
    Contributor

    did u save updates in your plunker?

  5. klinki commented on Dec 6, 2016

    @klinki
    Author

    This

        this._countriesService.getCountries(0).subscribe(data => this.countries = data);
        this._countriesService.getSelectedCountries(0)
          .subscribe(data => {
              let ids = data.map(item => item.id);
              let currentIds = this.countries.map(item => item.id);
              let indices = ids.map(id => currentIds.indexOf(id));
              
              let selection = indices.map(id => this.countries[id]);
              this.selectedCountries = selection;          
          });

    is workaround to get same instances of object.
    First call getCountries returns list of all available options. getSelectedCountries returns list of selected countries. But with different objects.

    So I get ids of selected objects. Then I also get IDs of all objects (currentIds) and I find out indices of items which should be selected. Then I map those indices into objects.

  6. klinki commented on Dec 6, 2016

    @klinki
    Author

    I saved my updates but for some reason they were not visible. I tried to freeze/unfreeze several times and now it should be visible. (I tested it in browser anonymous mode).

  7. DzmitryShylovich commented on Dec 6, 2016

    @DzmitryShylovich
    Contributor

    Ok now I see.

  8. DzmitryShylovich commented on Feb 2, 2017

    @DzmitryShylovich
    Contributor

    @klinki was fixed here #13349
    will be available in 4.0

  9. klinki commented on Feb 2, 2017

    @klinki
    Author
  10. DzmitryShylovich commented on Feb 4, 2017

    @DzmitryShylovich
    Contributor

    @klinki close the issue pls. thx

  11. angular-automatic-lock-bot commented on Sep 10, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  12. locked and limited conversation to collaborators on Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions