HTML5 Zone is brought to you in partnership with:

Christian is a dedicated Open Source developer and Entrepreneur. Currently he works a lot for the Apache Software Foundation. Besides he loves efficient working and regularly shares his thoughts on his blog, mostly tagged with "Zen Programming". Christian is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

Angular.js: ng-select and ng-options

12.04.2012
| 27864 views |
  • submit to reddit

Angular.js has a powerful directive waiting for you: it’s ng-select. With it you can fill an HTML-select box from your model. It does support a pretty cool selector syntax, which is – unfortunately – a little bit tricky.

Let’s assume you have a JSON stream which looks like that:

{
    "myOptions": [
        {
            "id": 106,
            "group": "Group 1",
            "label": "Item 1"
        },
        {
            "id": 107,
            "group": "Group 1",
            "label": "Item 2"
        },
...
}
 
// Get stream in the object called data
$scope.myOptions = data.myOptions;

The good thing is, you have a pretty flat data stream. When I started with this feature, I had an array of groups each containing the specific items. It was not possible for me to fill a select box that way. Instead, I refactored some of my code to what you can see above. Angular.js would care on the grouping on it’s own.

Here is my select definition:

<select
    ng-model="myOption"
    ng-options="value.id as value.label group by value.group for value in myOptions">
    <option>--</option>
</select>

The ng-model is the name of the property in which will reference my selection for future use. The ng-options is the directive which fills my dropdown. It deserves a bit more attention.

You will understand it easy when you read it from right to left. First there is: for value in myOptions. It means you’ll iterate through elements which are stored in myOptions. Every element will become available in this expression with the name “value”.

The next part is: group by value.group. This will tell Angular.js that it should make up tags and the label attribute will be filled with the content of the group field.

The last one is: value.id as value.label. In this case, value.id will become the model, if your users have chosen an option. If you would delete “value.id as” simply the whole value object would become the model. value.label does exactly what it looks like: it’s the label of the select box.

If you run your code, you’ll see something like that:

<optgroup label="Group 1">
   <option value="0">Item 1</option>
   <option value="1">Item 2</option>
</optgroup>

Please look again and check the value attribute of the options. You might have expected it’s matching the IDs from your JSON, but that is not the case (and yes, I thought like this initially). Actually this is an increasing number and references the position of the model (which is an array in my case). Don’t worry about that – if you select your option the correct ID will be selected and put into your model. Or, if you leave out the value.id part of the expression, the whole selected object will become your model.

You can easily test it.

<select
    ng-change="selectAction()"
    ng-model="myOption"
    ng-options="value.id as value.label group by value.group for value in myOptions">
    <option>--</option>
</select>

ng-change will fire if the user has chosen something. You can output it like that:

$scope.selectAction = function() {
    console.log($scope.myOption);
};

I find the syntax of ng-options pretty much counter intuitive. It took me a good while to understand it and I was glad about the nice help on the Angular.js mailinglist. That said, I think more examples on the docs and an improved syntax would really help. Like: foreach value in myOptions use value.label as label use value.id as model group by value.group.




Published at DZone with permission of Christian Grobmeier, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Dhananjay Tiwari replied on Tue, 2014/03/11 - 8:21am

Hi,

very informative and well-explained article. 

My JSON data format is

$scope.data={

"key_value1": {"name": "key_name1" },

"key_value2": { "name": "key_name2"}

};

and I want to make a select box with the following type of code:

view sourceprint?1.<select>2.    <option value="">Select an Option</option>3.    <option value="key_value1">key_name1</option>4.    <option value="key_value2">key_name2</option>5.</select>

also I want to access the selected option's value in my controller. How can I?

The code Link 

Thank You Very Much!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.