[英]angularJS - add a Static option with ng-options
I am using ng-options to print all the options for a form in my angular app. 我正在使用ng-options在我的角度应用程序中打印表单的所有选项。 I get the value directly from my database which gives me a list of countries:
我直接从我的数据库中获取了值,这给了我一个国家列表:
<select ng-options="country.country for country in countries" ng-model="selectedCountry" ng-change="updateSelectedCountry(selectedCountry)" class="form-control">
Here when the page is loaded, the select doesnt show anything, ie there is no placeholder whereas I'd like to print a static value like "anywhere" without having to add it in my "countries" list. 这里加载页面时,选择不会显示任何内容,即没有占位符,而我想打印一个静态值,如“任何地方”,而不必将其添加到我的“国家/地区”列表中。
I have tried this: 我试过这个:
<select ng-options="country.country for country in countries" ng-model="selectedCountry" ng-change="updateSelectedCountry(selectedCountry)" class="form-control">
<option>Anywhere</option>
</select>
But it's not working 但它不起作用
Does anyone have an idea how to solve this? 有谁知道如何解决这个问题?
Thanks 谢谢
This is probably a late post but you should almost never use ng-repeat where ng-options is better suited like this case because new scopes are created in ng-repeat and thus you'd have more overhead. 这可能是一个迟到的帖子,但你几乎不应该使用ng-repeat,其中ng-options更适合这种情况,因为新的范围是在ng-repeat中创建的,因此你会有更多的开销。
The solution to your problem is well written in the angular docs and what you need looks somewhat like 你的问题的解决方案很好地写在角度文档中,你需要的东西看起来有点像
<select ng-options="country.country for country in countries"
ng-model="selectedCountry"
ng-change="updateSelectedCountry(selectedCountry)"
class="form-control">
<option value="" disabled>Anywhere</option>
</select>
With this angular uses the value=""
to set a null value and starts iteration from after that value. 使用此角度使用
value=""
来设置空值并从该值之后开始迭代。
You could always just do this: 你总是可以这样做:
<select ng-model="selectedCountry" ng-change="updateSelectedCountry(selectedCountry)" class="form-control">
<option>Anywhere</option>
<option ng-repeat="country.country for country in countries">{{country.country}}
</option>
</select>
Here is my fiddle example 这是我的小提琴示例
Hope this helps! 希望这可以帮助!
I made a slight adjustment to hassassin 's fiddle. 我对哈萨辛的小提琴做了一点调整。 This is a little more inline with how
ng-options
is intended to work. 这与
ng-options
工作ng-options
更为一致。 Example 例
var myApp = angular.module('myApp', []) .controller('TestController', ['$scope', function ($scope) { $scope.data = [1,2,3]; $scope.sel = ''; }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="TestController"> {{sel}} <select ng-model="sel" ng-options="val for (key , val) in data"> <option value="">any</option> </select> </div> </div>
You should set the value attribute in the custom option tag, in your example should be: 您应该在自定义选项标记中设置value属性,在您的示例中应该是:
<select ng-options="country.country for country in countries" ng-model="selectedCountry" ng-change="updateSelectedCountry(selectedCountry)" class="form-control">
<option value='anyValueForThisOption'>Anywhere</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.