我在一个简单的用户注册表单中使用 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; )