简体   繁体   English

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

[英]adding a default option when using ng-options in Angularjs

I m having a dropdown for which I am using the ng-options to load the values from the model which is populated by the controller. 我正在使用ng-options下拉列表,以从控制器填充的模型中加载值。 Now I need to have one more option which is <option value='any'>any</option> which should be there as the selected option by default. 现在,我需要再有一个选项,它是<option value='any'>any</option> ,默认情况下该选项应作为所选选项存在。 How to do this in angular way? 如何以角度的方式做到这一点?

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

Plnkr link attached - Plnkr link 附加了Plnkr链接-Plnkr链接

@Pradeep, use @Pradeep,使用

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

plunkr link plunkr链接

You can simply add one more item in array and assing it to $scope.selected. 您可以简单地在数组中添加另一个项目并将其关联到$ scope.selected。 Plunkr . Plunkr

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

write option tag inside the select tag like select标签内写入option标签,例如

  <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"]  
    })

I have run in plunker its work please try this 我已经在工作中尝试了,请尝试一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM