angularJS - 使用 ng-options 添加静态选项 [英] angularJS - add a Static option with ng-options

查看:25
本文介绍了angularJS - 使用 ng-options 添加静态选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ng-options 在我的 angular 应用程序中打印表单的所有选项.我直接从我的数据库中获取值,该数据库为我提供了国家/地区列表:

I am using ng-options to print all the options for a form in my angular app. 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, i.e. there is no placeholder whereas I'd like to print a static value like "anywhere" without having to add it in my "countries" list.

我已经试过了:

<select ng-options="country.country for country in countries" ng-model="selectedCountry" ng-change="updateSelectedCountry(selectedCountry)" class="form-control">
<option>Anywhere</option>
</select>

但它不起作用

有人知道如何解决这个问题吗?

Does anyone have an idea how to solve 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>

这是我的fiddle示例

希望这会有所帮助!

这篇关于angularJS - 使用 ng-options 添加静态选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆