pro.mean pro.mean - 1 year ago 71
AngularJS Question

ng-options selected data return object

I have a array of object

{ id:x, name: 's' }
and using ng-options to create drop down with default selected data. This is working fine so far.

Now when I select one of the option and see the selected data, it return complete object but i need only name field as well as default value must be selected on load.

// in controller
vm.teams = [
id: 1,
id: 2,
id: 3,

vm.teamFormData = {
team: ''

vm.getTeam = function(formData) {
$log.debug(formData); << here I get object

// in View
<select ng-init=" = vm.teams[0]"
ng-options=" for t in vm.teams track by" >

Here is my working plunker

Answer Source

Your ngOptions syntax is slightly off - it's value as text for item in array

ng-options=" as for t in vm.teams track by"

And your ngInit then becomes

ng-init=" = vm.teams[0].name"

Side note - you probably don't want ngInit - there's no real use case in your above example for it. Instead, just set the property in the controller.

See plunkr:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download