我在简单的用户注册表单中使用angular-ui-select:
<ui-select ng-model="user.countryCode" convert-to-string theme="selectize" class="dropdown">
<ui-select-match placeholder="{{::strings('userDetails.countryPlaceholder')}}">{{$select.selected.name}}
</ui-select-match>
<ui-select-choices repeat="country in countries">
<span ng-bind-html="country.name | highlight: $select.search"></span>
</ui-select-choices>
</ui-select>
这是我的国家/地区数组定义:
$scope.countries = [
{name: 'Afghanistan', code: 'AF'},
{name: 'Albania', code: 'AL'},
{name: 'Australia', code: 'AU'},
{name: 'Austria', code: 'AT'},
{name: 'Azerbaijan', code: 'AZ'},
{name: 'Belarus', code: 'BY'},
{name: 'Belgium', code: 'BE'},
{name: 'Belize', code: 'BZ'},
{name: 'Benin', code: 'BJ'}
];
我在html中创建了用户对象,每个字段都有一个绑定到用户某些属性的ng-model。当我使用诸如firstName之类的简单输入时,它很容易:
<input class="form-control" type="text" name="firstName" ng-model="user.firstName"/>
但随着下拉 - 我希望国家/地区名称显示在下拉列表选项中,并将其代码放在用户对象中。 我想避免在控制器中编写代码。 (即$ scope.user.countryCode = $ scope.country.selected.code;)
答案 0 :(得分:12)
我认为你可以做到:
<ui-select-choices repeat="country.code as country in countries">
<span ng-bind-html="country.name | highlight: $select.search"></span>
</ui-select-choices>
从文档中: https://github.com/angular-ui/ui-select/wiki/ui-select-choices
重复ui-select-choices,识别你想要绑定的属性; repeat="item.id as item in cards">
。
答案 1 :(得分:0)
您可以使用自定义过滤器“透明地”将对象转换为数组:
答案 2 :(得分:-3)
//in your view(aaa.html) part /
<select
ng-model="Choices"
ng-options="choice.code as choice.name for choice in countries ">
//在您的控制器文件中 $ scope.user.countryCode =“选择”;