angular-select2
angular-select2 copied to clipboard
Object values instead of simple values
Is it possible to have objects as values instead simple values?
Here's a plunker: http://plnkr.co/edit/GgfVAn8M4DvAIWzLyI5b?p=preview
Should work, but an ng-model
directly on the scope won't:
$scope.foo = null;
$scope.bar = null;
https://egghead.io/lessons/angularjs-the-dot
I'm sorry, I don't understand what you mean. In my plunker you can see that with regular selectboxes I can use objects where with select2 it doesn't work. Whatever I choose from the list, the model is always set to the last entry in the list.
I'm sorry, I don't understand what you mean.
Check the video I linked. Anyway, that's not an angular-select2 issue, but you will be in trouble if you don't learn it.
As for objects: values in the select2 widget are keyed by their toString value, so if your objects define unique toString values, it should work. If not, I suggest you don't use objects as values (it's ugly anyway), use an index key.
Hey rubenv
Thank you for that awesome select2-wrapper!
I am running into the same problem as TheYves though.
AngularJS can assign whole objects to a variable in a select (which you say is ugly, can you elaborate on that?), but the directive assigns the object's toString-method as array key, which leads to the last item in the array being selected, no matter which element was clicked on in the dropdown.
Might I suggest a solution where the directive checks wether or not the value is a String or an object, and if it's an object check for an id (or even better, look for the "track by"-attribute of the query string)?
If you are open to the suggestion I can even write the functionality and make a pull request.
Regards
Might I suggest a solution where the directive checks wether or not the value is a String or an object, and if it's an object check for an id (or even better, look for the "track by"-attribute of the query string)?
Sure, track by makes total sense!
@DonHaron have you already implemented your suggestion? I would be very interested. :)
@TheYves I started it and made it work, but it was rubbish and not in line with the ngSelect directive, and broke other parts of the select2-directive. So I will have to do a proper rewrite based on ngSelect. I didn't get to it yet though...
Okay, thanks for the reply.
Any update on this issue?