-1

I have the following data items, that i would like to bind to a html select element and show the available answers

var array = [{ name: 'bob', isSelected: false}, {name: 'james', isSelected: false}];

The isSelected property on each item will either be true or false. If true, i want that item to be selected in the select element.

I also need the isSelected property to be updated to true/false depending on whether that item is selected or not.

I have tried using this, but nothing displays

<select ng-options="a.name for a in array">
</select>

Can someone please help?

Gillardo
  • 9,518
  • 18
  • 73
  • 141
  • If it is not a `select multiple` why do you need a `isSelected` property? The value of the `select` would be whatever name is selected. – Danny May 14 '14 at 13:06
  • Please only ask questions that have not been answered before on stack. – makeitmorehuman May 14 '14 at 13:09
  • Also you need an `ng-model` attribute for the value to be stored in for `ng-options` to work. I suggest you do it similar to http://jsfiddle.net/HB7LU/3629/ – Danny May 14 '14 at 13:14
  • Everyone is right about you needing ng-model to do the actual binding once you select something, but the main reason nothing is showing up in the select is because you have var array instead of $scope.array. – Ronald91 May 14 '14 at 13:16

2 Answers2

1

There isn't a binding in your example. Here is a fuller example from the AngularJS documentation: https://docs.angularjs.org/api/ng/directive/select. In other words, ng-model="" is required.

1

Instead storing each item's isSelected property, you should use the ng-model to retrieve the value of the <select>

for example:

<select ng-model="result" ng-options="a.name for a in array" multiple>
</select>

You selection will bind result. in this case. if you select first option. result will be [{name: 'bob'}].

Note that a multiple select's result is always an array

Bob Yuan
  • 588
  • 3
  • 14