angularjs select

1. 第一种方法
$scope.colors = [
  {name:'black', shade:'dark'},
  {name:'white', shade:'light'},
  {name:'red', shade:'dark'},
  {name:'blue', shade:'dark'},
  {name:'yellow', shade:'light'}
];
$scope.myColor =  $scope.colors[1]; // cannot be {name:'white', shade:'light'}


<select ng-model="myColor" ng-options="color.name for color in colors">
  <option value="">-- choose color --</option>
</select>

2. 第二种方法
$scope.colors = [
  {name:'black', shade:'dark'},
  {name:'white', shade:'light'},
  {name:'red', shade:'dark'},
  {name:'blue', shade:'dark'},
  {name:'yellow', shade:'light'}
];
$scope.myColor =  'red'; // red


<select ng-model="myColor" ng-options="color.name as color.name for color in colors">
  <option value="">-- choose color --</option>
</select>

+1 天

猜你喜欢

转载自hongyegu.iteye.com/blog/2283319