在Angularjs中使用ng-options时添加默认选项

时间:2016-01-08 05:39:24

标签: angularjs angularjs-ng-options

我有一个下拉列表,我使用ng-options来加载控制器填充的模型中的值。现在我需要再添加一个<option value='any'>any</option>选项,默认情况下该选项应该作为选定选项。如何以棱角分明的方式做到这一点?

<body ng-controller="myController">
  <h1>States in India!</h1>
  <select ng-options="state for state in states" ng-model="selectedState">      </select>
</body>

附加了Plnkr链接 - Plnkr link

3 个答案:

答案 0 :(得分:1)

@Pradeep,使用

    <select ng-options="state for state in states" ng-model="selectedState">
        <option value="">-- choose state --</option>
    </select>

plunkr link

答案 1 :(得分:0)

您只需在数组中添加一个项目并将其分配给$ scope.selected。 Plunkr

$scope.selectedState = "Select";
$scope.states=["Select","Karnataka", "Tamilnadu", "Andra-Pradesh", "Rajasthan"]

答案 2 :(得分:0)

option标记内写select标记,如

  <body ng-controller="myController">
    <h1>States in India!</h1>
    <select  ng-options="state as state for state in states" ng-model="selectedState">
         <option value=''>any</option>
    </select>
  </body>


angular.module("myApp", []).

    controller("myController", function($scope){
      $scope.states=["Karnataka", "Tamilnadu", "Andra-Pradesh", "Rajasthan"]  
    })

我在plunker中运行它的工作请尝试这个