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.

ng-options track by and select as are not compatible #6564

Closed

Description

@damiengenet

When both a track by expression and a select as expression are used in ng-options, binding doesn't work anymore.

For instance:

      it('should bind to scope value through experession while tracking/identifying objects', function() {
        createSelect({
          'ng-model': 'selected',
          'ng-options': 'item.id as item.name for item in values track by item.id'
        });

        scope.$apply(function() {
          scope.values = [{id: 10, name: 'A'}, {id: 20, name: 'B'}];
          scope.selected = scope.values[0].id;
        });

        expect(element.val()).toEqual('10');

        scope.$apply(function() {
          scope.selected = scope.values[1].id;
        });

        expect(element.val()).toEqual('20');
      });

It seems that in the ngOptions directive, the track function always directly use the value expression even if a select as expression is set.

Activity

  1. added this to the milestone on Mar 8, 2014
  2. janv commented on May 13, 2014

    @janv

    Oops, accidentally deleted my previous comment.
    I created/modified a fiddle to illustrate the problem:
    http://jsfiddle.net/qWzTb/418/

  3. SQiShER commented on May 13, 2014

    @SQiShER

    +1 I'm having the same problem.

  4. janv commented on May 13, 2014

    @janv

    It's annoying but you can really just leave away the track by and it'll work.

  5. ahoereth commented on Jul 15, 2014

    @ahoereth

    Can confirm this problem still exists as of v1.3.0-beta.15.

  6. wojciechfornal commented on Jul 21, 2014

    @wojciechfornal
    Contributor

    Having the same problem with 1.2.20 :/

  7. mvalim commented on Aug 6, 2014

    @mvalim

    Same here

  8. added a commit that references this issue on Aug 8, 2014
  9. janv commented on Aug 8, 2014

    @janv

    I've been encountering this again.
    The problem was created in c32a859 by @quazzie:

    For checking if an option is selected, the trackFn is used, where the valueFn should be used instead. I'll try to come up with a fix.

  10. janv commented on Aug 8, 2014

    @janv

    Ok, I have investigated this. It's not actually a bug, just misleading documentation.
    The thing is, you just can't combine value as label for collection with track by.
    You have to pick the one or the other. There are two different Use Cases:

    1. You want a shiny presentation for an ugly value

    Use value as label:

    items = [
      {value: 1, label: 'One'},
      {value: 2, label: 'Two'},
    ]
    template = 'ng-options="item.value as item.label for items"'
    

    When you change the dropdown, the model is assigned whatever you define in front of the as.

    2. You want to iterate over complex objects

    Use track by:

    items = [
      {id: 1, ...},
      {id: 2, ...},
      {id: 3, ...},
    ]
    template = 'ng-options="item as item.label for items track by items.id"'
    

    When you change the dropdown, the model is assigned the item. The track by expression is only used to associate options with items through the value attribute on the <option>.

    Illustrated in http://jsfiddle.net/0vpsv1wa/1/

  11. 39 remaining items

  12. added a commit that references this issue on Oct 10, 2014
    526498a
  13. jeffbcross commented on Oct 10, 2014

    @jeffbcross
    Contributor

    Thanks for bringing the issue up, and thanks for the feedback on the approaches. This is a tough problem without a clean solution that satisfies all constraints, is consistent with ng-repeat, and is backwards-compatible. In the meantime, I removed the thrown error, added tests for how track by and select as should work together, and fixed the implementation.

    Unfortunately, the originally reported issue would not work with the expression/model as is, but could work with some re-working.

  14. jonathansolorzn commented on Nov 19, 2015

    @jonathansolorzn

    This is still happening in v1.4.7, no fix yet?

  15. mariusstaicu commented on Nov 28, 2015

    @mariusstaicu

    same problem here!!

  16. agvalente commented on Dec 1, 2015

    @agvalente

    Same problem!!

  17. DanieleSassoli commented on Dec 8, 2015

    @DanieleSassoli

    same thing here with in v1.4.7

  18. dlazzarino commented on Mar 14, 2016

    @dlazzarino

    Same thing with v1.4.8

  19. aj07mm commented on May 5, 2016

    @aj07mm

    Same thing with v1.5.3

  20. hebergentilin commented on May 10, 2016

    @hebergentilin

    The ng-options works fine, but when update the model manual, the select value hadn't update.

    <select ng-model="shiny"
                    ng-options="opt.value as opt.label for opt in options">
                </select>
    
    $scope.complex = 2;
    

    See the http://jsfiddle.net/0vpsv1wa/110/

  21. gkalpak commented on May 13, 2016

    @gkalpak
    Member

    There are certain (documented) limitations in using track by together with select ... as.
    Not all usecases are supported.

  22. mgsstms commented on Jan 29, 2017

    @mgsstms

    ng-options="item.id as item.name for item in values track by item.id" works, but doesn't se the default value
    http://plnkr.co/edit/6YdeJviPnCwY3Em9ltyo?p=preview

  23. gkalpak commented on Jan 29, 2017

    @gkalpak
    Member

    No, it doesn't work (i.e. track by has no effect).

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions