最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angular- ui-select - how to bind object property to ng-model - Stack Overflow

programmeradmin5浏览0评论

i'm using the angular-ui-select in a simple user registration form:

<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>

Here's my countries array definition:

$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'}
];

I'm creating the user object in my html, every field had an ng-model binded to some property of the user. When i'm using simple input such as firstName then it's easy:

<input class="form-control" type="text" name="firstName" ng-model="user.firstName"/>

But with the dropdown - I want the country name to be displayed in the dropdown list options and the its code to be placed in the user object. I want to avoid writing code in the controller for this. (i.e. $scope.user.countryCode = $scope.country.selected.code; )

i'm using the angular-ui-select in a simple user registration form:

<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>

Here's my countries array definition:

$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'}
];

I'm creating the user object in my html, every field had an ng-model binded to some property of the user. When i'm using simple input such as firstName then it's easy:

<input class="form-control" type="text" name="firstName" ng-model="user.firstName"/>

But with the dropdown - I want the country name to be displayed in the dropdown list options and the its code to be placed in the user object. I want to avoid writing code in the controller for this. (i.e. $scope.user.countryCode = $scope.country.selected.code; )

Share Improve this question asked Jun 23, 2015 at 16:12 Tamar CohenTamar Cohen 1,2922 gold badges17 silver badges29 bronze badges 2
  • I cannot get the point, you just use user.countryCode as ng-model for the select, so everything you select is bind to $scope.user.countryCode without doing anything else – michelem Commented Jun 23, 2015 at 16:30
  • 1 $sope.user.countryCode holds the hole country object (name + code), while I want it to have the country code only – Tamar Cohen Commented Jun 24, 2015 at 6:31
Add a ment  | 

3 Answers 3

Reset to default 16

I think you can do:

<ui-select-choices repeat="country.code as country in countries">
    <span ng-bind-html="country.name | highlight: $select.search"></span>
</ui-select-choices>

From the documentation: https://github./angular-ui/ui-select/wiki/ui-select-choices

Example: Binding Single Property

In the repeat of the ui-select-choices identify the property you are wanting to bind to; repeat="item.id as item in cards">.

You can use custom filter to "transparently" transform your object into an array :

https://code.angularjs/1.4.7/docs/error/filter/notarray

https://github./petebacondarwin/angular-toArrayFilter

//in your view(aaa.html) part /
<select 
ng-model="Choices" 
ng-options="choice.code as choice.name for choice in countries ">

// in your controler file $scope.user.countryCode = "Choices";

发布评论

评论列表(0)

  1. 暂无评论