Bind to multiple models

25 views
Skip to first unread message

Mark Ferguson

unread,
Jun 7, 2013, 1:29:16 PM6/7/13
to ang...@googlegroups.com
Is there a way to bind to multiple models in AngularJS?

This is the case I'm dealing with: I have a select list, whose options are composed of objects; say a person for example { id: 5, name: 'John', phone: '555' }. I want to bind the id value from the select list to an attribute on a model object. Note that I don't want to store the entire person, but just the ID value.

The issue is that later on, I'd like to be able to get a reference to the currently selected object. Currently I only have its ID value since that is what I used for binding, and I need to find the object by searching the source array.

It seems that there should be a way to bind the id value from the selected option to the model that I need to keep track of, as well as to another place that keeps track of the entire reference. Is anything like this possible?

Andy Joslin

unread,
Jun 7, 2013, 1:41:57 PM6/7/13
to ang...@googlegroups.com
You could bind the whole model, and then on selection read the ID from it: http://plnkr.co/edit/IyzBMYXnbpoQrLLEwu9x

Mark Ferguson

unread,
Jun 7, 2013, 7:07:12 PM6/7/13
to ang...@googlegroups.com
I think this will work. I do end up with the opposite issue where I need to locate the whole object in the array when setting the value initially on load, but I think I prefer this option. Thanks!
Reply all
Reply to author
Forward
0 new messages