我需要在我的angular-breeze应用程序中使用一个国家下拉菜单,我尝试了以下方式:https://github.com/banafederico/angularjs-country-select
这里的问题是它没有国家/代码对,只有国家的全名,我不想将其保存到数据库中。是否有其他的angular指令/服务可以用来填充一个包含国家的下拉菜单? 我已经做了一些研究,但我没有找到任何可供angular使用的现成的国家选择选项。
您可以使用ng-repeat
或ng-options
指令创建国家下拉菜单。通过这种方式,您可以完全控制。如果需要在多个地方使用该元素,则可以将其创建为指令。
Demo: http://plnkr.co/edit/2y9Jcektl8g2L0VoNQyp?p=preview
使用ng-options指令
<select ng-model="country" ng-options="country.name for country in countries track by country.code">
<option value="">-- Select a Country --</option>
</select>
使用ng-repeat指令:
<select ng-model="country">
<option value="">-- Select a Country --</option>
<option ng-repeat="country in countries" value="{{country.code}}">{{country.name}}</option>
</select>
您的控制器中的国家范围:
$scope.countries = [
{name: 'Afghanistan', code: 'AF'},
{name: 'Åland Islands', code: 'AX'},
{name: 'Albania', code: 'AL'},
{name: 'Algeria', code: 'DZ'},
{name: 'American Samoa', code: 'AS'}
];
select
、ng-model
和ng-options
来创建下拉菜单。你不需要其他任何东西。 - JB Nizet