Categories
Uncategorized

AngularJS 1.X ng-options

Using the predefined angularjs ng-options directive can be quite tricky at times. The following is a basic example of how to iterate through list of people. The value of the person that is chosen from the drop down list is store in $scope.chosenPerson and can be accessed with the same from the controller.
Example 1:


  

Output:
ng-options 1

Demo

Example 2: If you just want the value to be last name instead of the whole object  use this.


  

Output:
ng-options 1

Demo

To access a nested array and show it as part of the dropdown the following example demonstrates how to do that,

Example 3


  

Output

ng-options output2

Demo

In the above example if the countries and locations were 2 completely different objects, then a hashmap can be used to link those two objects by keys and values.