AngularJS Select(选择框)

AngularJS 可以使用数组或对象创建一个下拉列表选项。


使用 ng-options 创建选择框

在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出,如下实例:

<div ng-app="myApp" ng-controller="myCtrl">
  <select ng-init="selectedName = names[0]" ng-model="selectedName" ng-options="x for x in names"></select>
</div>
<script>var app = angular.module('myApp', []);
  app.controller('myCtrl',
  function($scope) {
    $scope.names = ["Google", "w3cworlds", "Taobao"];
  });</script>

ng-init 设置默认选中值。


ng-options 与 ng-repeat

我们也可以使用ng-repeat 指令来创建下拉列表:

<select>
<option ng-repeat="x in names">{{x}}</option>
</select>

 

  • 赞助商