Issue
https://angularjsaz.blogspot.com/2015/09/tutorial-28-angularjs-listbox-example.html
That shows:
<select style="width: 100%;" size="7" ng-model="currentItems" multiple
ng-multiple="true"
ng-options="opt as opt for opt in months">
</select>
This works. I want to understand what does this statement do: opt as opt for opt in months
I also know that ng-repeat takes car in cars. Why can't I use such a thing here?
In my case, cars was:
cars = [
{make: 'Mazda', model: 'Miata', year: 2001},
{make: 'Toyota', model: 'Prius', year: 2013},
]
In their case, months is:
months = ['January','February','March','April']
Solution
According to the documentation, ng-options for iterating an array data source (as in your case) follows the following pattern:
select as label for value in array
Where:
array: an expression which evaluates to an array / object to iterate over.
value: local variable which will refer to each item in the array or each property value of object during iteration.
label: The result of this expression will be the label for element. The expression will most likely refer to the value variable (e.g. value.propertyName).
select: The result of this expression will be bound to the model of the parent element. If not specified, select expression will default to value.
This is mainly used for displaying arrays of objects as options in a select list, where you want the object being iterated over, the label, and the value that's bound to the ng-model
directive to be 3 different things. In your case, they're just strings, so it just basically is saying stringSelectValue as stringLabel for stringItem in stringArray
.
You can simplify this example by saying ng-options="month for month in months"
. If a select value is not provided, it will use the label value as the select value, too.
Answered By - mhodges
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.