Angular ui-select模型绑定

时间:2016-05-20 09:00:08

标签: javascript angularjs ui-select

我在范围

中有以下对象
$scope.myData = {};
$scope.companies =    [{name:'Microsoft',slug:'micro'},
                {name:'Google',slug:'gg'}];

我正试图通过使用ui-select

来敲击模型
<div class="form-group">
  <label>Company</label>
    <ui-select ng-model="$parent.myData.company" theme="bootstrap">
    <ui-select-match placeholder="Select or search a company in the list...">{{$select.selected.name}}</ui-select-match>
    <ui-select-choices repeat="company in companies | filter: $select.search">
    <div ng-bind-html="company.name | highlight: $select.search"></div>
    </ui-select-choices>
    </ui-select>
</div>

但是,当我选择公司时,myData.company会变成完整的对象,例如。 {"name":"Microsoft","slug":"micro"}如何将slug公司对象绑定到myData.company但能够按公司名称搜索?

1 个答案:

答案 0 :(得分:2)

由于this我终于找到了如何做到这一点为了使其有效,<ui-select-choices>应该按如下方式更改;

<ui-select-choices repeat="company.slug as company in companies | filter: $select.search">