首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在angular-ui-select中将$index绑定到ng-model

在angular-ui-select中将$index绑定到ng-model
EN

Stack Overflow用户
提问于 2016-04-20 20:45:46
回答 3查看 1.5K关注 0票数 1

我试图使用ui-select指令将$index绑定到ng-model,但没有成功。

代码语言:javascript
复制
<ui-select ng-model="selected.m">
    <ui-select-match>
      <span ng-bind="$select.selected.name"></span>
    </ui-select-match>
    <ui-select-choices repeat="$index as choice in itemArray">
      <span ng-bind="choice + '' + $index"></span>
    </ui-select-choices>
  </ui-select>

在上面的模板中,itemArray是一个月份名称数组,在从下拉列表中选择任何月份时,我希望将其$index绑定到ng-model (即.'selected.m')。

我已经让this plunker准备好了。

EN

回答 3

Stack Overflow用户

发布于 2016-04-20 20:56:03

我找到了解决这个问题的方法:

代码语言:javascript
复制
  <ui-select ng-model="dummy"  ng-change="selected.m=itemArray.indexOf(dummy)">
    <ui-select-match>
      <span ng-bind="$select.selected.name"></span>
    </ui-select-match>
    <ui-select-choices repeat="choice in itemArray">
      <span ng-bind="choice + '/' + $index"></span>
    </ui-select-choices>
  </ui-select>

这是必要的,因为$index仅在表达式的track by或循环内部可用。此外,AngularJS是一个框架,它希望你操纵对象,而不是像以前那样索引,这就是为什么我认为ng-repeat/ng-options不是为此而设计的。

票数 0
EN

Stack Overflow用户

发布于 2016-04-21 21:18:32

如果你想要的只是$index,那么你可以这样做:

代码语言:javascript
复制
<ui-select-choices repeat="$index in itemArray">
  <span ng-bind="itemArray[$index] + '' + $index"></span>
</ui-select-choices>

这会将模型设置为$index,这将更新您的ng- $select.selected。

代码语言:javascript
复制
<pre> {{ selected }} </pre>

<ui-select ng-model="selected.m">
  <ui-select-match>
    <span>{{$select.selected}}</span>
  </ui-select-match>
  <ui-select-choices repeat="$index in itemArray">
    <span ng-bind="itemArray[$index] + '' + $index"></span>
  </ui-select-choices>
</ui-select>
票数 0
EN

Stack Overflow用户

发布于 2018-02-07 19:34:32

代码语言:javascript
复制
<ui-select ng-model="vm.mySelectedCountry" title="Choose a country" ng-required="true"> 
        <ui-select-match placeholder="">{{vm.countries[vm.mySelectedCountry].countryName}}</ui-select-match>
        <ui-select-choices repeat="$index in vm.countries| filter: $select.search">
                <span ng-bind-html="vm.countries[$index].countryName | highlight: $select.search"></span>
        </ui-select-choices>
</ui-select>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36744241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档