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

#8761 don't work if key is object #9383

Closed
Delagen opened this issue Oct 2, 2014 · 7 comments
Closed

#8761 don't work if key is object #9383

Delagen opened this issue Oct 2, 2014 · 7 comments
Assignees
Milestone

Comments

@Delagen
Copy link

Delagen commented Oct 2, 2014

#8761 does not work when ngModel value is object

Look at console
http://jsbin.com/buvowimipale/5/
I think optionsMap should be array of values.

@gkalpak
Copy link
Member

gkalpak commented Oct 2, 2014

Setting the model to '3' shouldn't work anyway.

There are two different issues going on:

  1. When using ngOptions, the value of each <option> is not bound to the actual modelValue associated with that option, but to the index of the option in the options list. Thus it seems to work when looking for '3' (because there are 4 options and hence the 4th has an index of 3).
  2. Even when corrctly assigning model1 to $scope.options[2].key, hasOption returns false, because the modelValue contains the extra $hashKey key added by Angular, while the corresponding <option>'s value contains a JSONified version of the object (with all $-prefixed properties removed).
    If you assign the model to angular.toJson($scope.options[2].key), then hasOption return true (as expected).

I am not really sure what is the purpose of hasOption() and how/if this issue should be dealt with.

@Delagen
Copy link
Author

Delagen commented Oct 2, 2014

I think that optionsMap generated with selectCtrl should be a little rewritten.
Instead of setting property with the name of key to true value it may be an array of values.
hasOptions may call Array.some method to resolve if key belongs to options. It may be a little slower but it should work.
May be I am wrong.
For example:

var  
...
optionsMap = {},
...
self.addOption = function(value) {
...
        optionsMap[value] = true;
...

self.hasOption = function(value) {
        return optionsMap.hasOwnProperty(value);
      };
...
 self.removeOption = function(value) {
        if (this.hasOption(value)) {
          delete optionsMap[value];

change to

optionsMap = [],
...
self.addOption = function(value) {
...
        optionsMap.push(value);
...
self.hasOption = function(value) {
        return optionsMap.some(function(mapValue){return mapValue==value});
      };
...
 self.removeOption = function(value) {
        if (this.hasOption(value)) {
          optionsMap.splice(optionsMap.indexOf(value),1);

But this is not full. So it is not full solution.

PS. Found solution update in the latest commit

selectCtrl.addOption(option.id, element);

instead of

selectCtrl.addOption(option.label, element);

PSS. This is not solution because option.id is just the index but not key

PSSS. This can be

selectCtrl.addOption(option.id!==""&&valuesFn(scope)[option.id].key||null, element);

but i am not sure that is the best solution.
the same must be changed in

selectCtrl.removeOption(option.label);

@jeffbcross jeffbcross self-assigned this Oct 9, 2014
@jeffbcross
Copy link
Contributor

Can you test with latest snapshot (https://code.angularjs.org/snapshot/angular.js) and see if you're still experiencing issues? This logic was recently fixed by @shahata. I'm having a hard time understanding what is broken here.

@jeffbcross jeffbcross added this to the Purgatory milestone Oct 9, 2014
@jeffbcross jeffbcross removed their assignment Oct 9, 2014
@Delagen
Copy link
Author

Delagen commented Oct 10, 2014

@jeffbcross No. It still not working. Works only if key is not object
http://jsbin.com/buvowimipale/9/

@Delagen
Copy link
Author

Delagen commented Nov 21, 2014

Any progress???
Two month passed

@Narretz Narretz self-assigned this Jan 14, 2015
@Narretz
Copy link
Contributor

Narretz commented Jan 15, 2015

In 1.3.9, there was a huge update to ngOptions, please test again.

I also have problems seeing what's wrong in the jsbin, it looks good to me.

@Narretz Narretz closed this as completed Jan 15, 2015
@Narretz
Copy link
Contributor

Narretz commented Jan 15, 2015

Sorry, correction - the big update is in the unstable branch (1.4.0-beta.0). Anyway, please feel free to reopen with a reproduction that clearly shows the issue. Thank!

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

4 participants