-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(select,ng-options): New expression (identify by) in ng-options #2040
Conversation
This would be a godsend for my application, as I'm using Mongo and having to do an additional look-up based on an identifier seems sloppy. FYI: Knockout has similar capabilities by setting optionText and optionValue bindings. Works like a charm. PS: I was impatient, and so I cloned your branch and am using this now (not in a production environment, but I'm really hopeful this makes it in!) |
Adds a new expression (identify by) in ng-options. This can be used when working with objects. You can now write: ng-options="obj identify by obj.id as obj.name for obj in objects" The identify part will be used when checking for equality of objects. Examples: <select ng-model="user.favMovieStub" ng-options="movie identify by movie.id as movie.name for movie in movies"> </select> scope: { user: { name: 'Test user', favMovieStub: { id: 1, name: 'Starwars' } } movies: [{ id: 1, name: 'Starwars', rating: 5, ... }, { id: 13, ... }] } Will match user favMovieStub to the first movie in the movies array.
This patch (or some alternative of it) should definitely be accepted. Without it, a lot of unnecessary boilerplate code is needed. |
Is this patch likely to be merged? It would be a great help for me, too. |
+1, why this patch isn't accepted or closed yet? |
It ought to be using a syntax similar, or compatible to, the new ng-repeat syntax: Master Repeat Docs |
@petebacondarwin do you mean |
Sure, i'm not picky on syntax. I'll take a look at changing it tomorrow. |
Uhmm should it be |
Whatever it is we should try to maintain some level of consistency across the various comprehension syntaxes. This
But again we need someone in the core team to chime in here. |
I agree that we need some input from core team. |
+1 for @petebacondarwin's syntax. let's go forward with that. |
Was it this correct to add a commit ? |
Uhmm it looks like it got merged with my other patch (backbutton-locationchangestart)... |
update test for new expression
Think i fixed it... ? :) |
PR Checklist (Minor Feature)
|
Landed at: c32a859. Thanks! |
Add a new expression (identify by) in ng-options.
This can be used when working with objects. Instead of objects comparing by reference
it will compare by ex. a property.
So you can write:
The identify part will be used when checking for equality of objects.
Example:
If you get an json object from server with nested objects and a collection of those nested
objects you can use this to select correct object.
This will correctly select building with id 1 in the select and when select is changed
it will populate the user with the building object selected.