DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Angular.js: ng-select and ng-options

Angular.js: ng-select and ng-options

Christian Grobmeier user avatar by
Christian Grobmeier
·
Dec. 04, 12 · Interview
Like (0)
Save
Tweet
Share
6.87K Views

Join the DZone community and get the full member experience.

Join For Free

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.




IT Syntax (programming languages) JSON Label Directive (programming) Stream (computing) Attribute (computing) Data structure Element

Published at DZone with permission of Christian Grobmeier, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Policy-as-Code? An Introduction to Open Policy Agent
  • What Was the Question Again, ChatGPT?
  • Utilize OpenAI API to Extract Information From PDF Files
  • The Role of Data Governance in Data Strategy: Part II

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: