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

ngSelect remove the wrong option when using an object implementing a specific toString as value #9414

Closed
joelross opened this issue Oct 3, 2014 · 1 comment

Comments

@joelross
Copy link

joelross commented Oct 3, 2014

Hello,

So, context:
I have objects with an field 'id' (a string).
I list those objects in a ngSelect directive with ng-option. I use the field 'id' as label, and the object itself as value (I use ng-model to retrieve this value in the controller).
Those objects implement a toString method that simply return the field 'id'.
Nothing is initially selected in the ngSelect.

Problem: when I try to select an object using the ngSelect from the interface, it is (visually) unselected right away.

Expected cause: in the function removeOption (I suspect there is the same issue in addOption)

      self.removeOption = function(value) {
        if (this.hasOption(value)) {
          delete optionsMap[value];
          if (ngModelCtrl.$viewValue == value) {
            this.renderUnknownOption(value);
          }
        }
      };

The condition ngModelCtrl.$viewValue == value evaluates to true (this non-strict comparison uses the toString method of the object stored in $viewValue, which unfortunately for me, evaluates to the same value as the label).

Thanks in advance for your time.

[edit]
Actually, could not reproduce the bug in a jsfiddle for demo... Will try to see what went wrong.

[edit 2]
Ok, the bug is present in 1.3.0-rc4 but not in 1.2.1 apparently.

correct behaviour, without toString
faulty behaviour, with toString
(try to select the value and see what happen to the select)

@shahata
Copy link
Contributor

shahata commented Oct 4, 2014

@joelross - Thanks for posting this. This is indeed a regression that was introduced in rc4. It is described in a bit more detail in #9418 and there is a pending PR #9421 with a fix. I'm going to close this as duplicate, but feel free to reopen if you think that this issue is something different.

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

2 participants