ng-options with object

Description

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 = [<!--   ww  w  .  j a v a  2s  . co 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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities