DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

How to setup angularjs select to work with objects

Code:

<select ng-model="ticket.owner.id" 
        ng-options="user.id as getFullName(user) for user in users" required>
</select>

The above example actually works. However when I send the modified object to my rest api, the only changed value of the object is the ID. E.g. when option 1 is selected and I select option2 and submit, the object associated to option1 is send, but with the ID of option2.

When I have:

<select ng-model="ticket.owner" 
        ng-options="user as getFullName(user) for user in users" required>
</select>

and change the selected value and submit, the correct object is transmitted to rest api. However the select box is broken, it shows an empty field and does not set the selected value.

Any ideas we can use the select box to select objects but not just ID's?

Upvotes: 3

Views: 12850

Answers (1)

Tosh
Tosh

Reputation: 36030

Not sure your implementation of getFullName() and what is actually getting sent to the rest service.

http://plnkr.co/edit/U7WIe4CvPUQFJFE4NtB6?p=preview

You can see in this example, ticket.owner being one of the object in users array that is chosen by the select box. And selection box shows the full name of the users.

Is this what you want?

Upvotes: 4

Related Questions