angularjs - How to bind ng-model to the "value" of selected item for ui-select -
please check out following plunk: http://plnkr.co/edit/fc9xcyyygtak0agvv35t?p=preview
<ui-select ng-model="fm.countrycode" id="countrycode"> <ui-select-match placeholder="select country...">{{$select.selected.label}}</ui-select-match> <ui-select-choices repeat="item in countries | filter: $select.search" value="{{$select.selected.value}}"> <div ng-bind-html="item.label | highlight: $select.search"></div> <small ng-bind-html="item.value | highlight: $select.search"></small> </ui-select-choices> </ui-select>
i'm trying ui-select set fm.countrycode value of selected item. it's setting fm.countrycode whole country item. example if select afghanistan, fm.countrycode set {"value":"af","label":"afghanistan"}. want "af".
do have set $watch achieve or there more elegant solution?
you can change ui-select to:
<ui-select ng-model="fm.countrycode" id="countrycode"> <ui-select-match placeholder="select country...">{{$select.selected.label}}</ui-select-match> <ui-select-choices repeat="item.value item in countries | filter: $select.search" value="{{$select.selected.value}}"> <div ng-bind-html="item.label | highlight: $select.search"></div> <small ng-bind-html="item.value | highlight: $select.search"></small> </ui-select-choices> </ui-select>
this take item.value complete item, instead of both value , label.
so repeat part: <ui-select-choices repeat="item.value item in countries
trick.
Comments
Post a Comment