Ng-Options-2

 

In this example ng-options directive generate list of students using array of object "students".The selected student id will show in pre element because selected student id bound to the StudentId variable with ng-model directive.

 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to LearnKode - A code learning platform</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> </head> <body ng-app="Example"> <div ng-controller="ExampleController" ng-init="StudentId=1"> Students: <select ng-model="StudentId" ng-options="student.id as student.name for student in students"></select> <pre>studentId: {{StudentId}}</pre> </div> <script> var app = angular.module("Example", []); app.controller('ExampleController', ['$scope', function ($scope) { $scope.students = [{ name: 'Herry', id: 1 }, { name: 'John', id: 2 }, { name: 'Peter', id: 3 }]; }]); </script> </body> </html>
Output