1

I was just going through this example online of $watch in angular and basically came across this weird syntax below:

       <select ng-model="countSelection" ng-options="n for n in [5,10,15,20]"></select>

Now i am familiar with the for ..in loop, in native javascript , but what the heck is ng-options="n for n in [5,10,15,20]" ? can anybody explain this ? I have seen the documentation HERE , there are a few similar examples , but nothing quite the same, and i quite puzzled by that syntax, can anybody explain ?

Thank you.

3
  • Read the proper documentation docs.angularjs.org/api/ng/directive/ngOptions Commented Oct 25, 2015 at 10:44
  • Please follow-up your questions by either accepting an answer or commenting on them to clarify why you haven't accepted any answer yet. Commented Oct 25, 2015 at 12:06
  • There is no time frame for accepting an answer. don't get desperate for your answer to be accepted. this question is going nowhere and is open for discussion. Commented Oct 25, 2015 at 15:42

3 Answers 3

1

Basically it's not JavaScript, it's a repeat expression that is specific to AngularJS. Here's the definition from the documentation:

The expression indicating how to enumerate a collection.

There are various formats for repeat expressions. AngularJS compiler is able to parse them and provide variables for a loop scope.

Sign up to request clarification or add additional context in comments.

2 Comments

can you provide a documentation link or an example . thanks
@TenaliRaman It's on the page you posted. Scroll down to the arguments section.
0

Yes, there is tricky and specific implementation of ngOptions for dropdowns, actually, it is documented here.

Long story short: you have an array, and you should tell Angular, what it should pass as value for those options. For example, you have array of objects:

myArr = [{id:1, description:'First'}, {id:2, description:'Second'}, ...]

So if you want to use field ID as a value for select options, you can create the following select:

<select ng-model="countSelection" ng-options="obj.id for obj in myArr"></select>

That's it! In your case, you have the array of numbers, and you want to use this numbers as value for your options. So we can translate this line ng-options="n for n in [5,10,15,20]" as following:

Please, take every element of the array, and call it 'n'. And please use this 'n' as a value for my options.

Comments

0

One reason is that it can easily be used to iterate over an array of objects based on a particular key. Say you have the following data:

$scope.arr = [
    {'key1': 1, 'key2': 4},
    {'key1': 2, 'key2': 5}
]

Now, in your markup you can either iterate over key1:

<select ng-model="temp" ng-options="n.key1 for n in arr">

</select>

or over key2

<select ng-model="temp" ng-options="n.key2 for n in arr">

</select>

This ensures your ng-model holds on to the right key's value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.