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.

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

Closed
@joelross

Description

@joelross

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)

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