AngularJS Tutorial - ng-options with object








The following code shows how to use ng-options with object.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
var TestCtrl = function($scope) {
   $scope.colors = [<!--from  w w w. j av  a 2 s.c o m-->
    {name:'black', shade:'dark'},
    {name:'white', shade:'light'},
    {name:'red', shade:'dark'},
    {name:'blue', shade:'dark'},
    {name:'yellow', shade:'light'}
  ];
  $scope.color = $scope.colors[2];
}
</script>
</head>
<body ng-app>
  <div ng-controller="TestCtrl">
  <select ng-model="color" ng-options="c.name for c in colors"></select><br>
</div>
</body>
</html>

The code above is rendered as follows: