angularjs - 如何根据其属性之一的值在控制器得到一个项目的索引,在NG重复过滤器? [英] angularjs - how to get in the controller the index of an item in a ng-repeat filter based on the value of one of its properties?

查看:200
本文介绍了angularjs - 如何根据其属性之一的值在控制器得到一个项目的索引,在NG重复过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的NG-重复在我的HTML文件中显示过滤的项目:

I use a ng-repeat in my html file to display filtered items:

<li ng-repeat="item in (filteredItems = (items | filter:query))">
  {{ item.name }}
</a>

在控制器中,我希望以自己的财产之一,得到一个项目的索引。

In the controller, I'd like to get the index of an item based on one of his property.

precision:我想获得在指数过滤列表并没有在整个列表

Precision: I'd like to get the index in the filtered list and not in the whole list.

这里有个例子,这将是该项目巫名的指数 some_item_7

Here for example, it will be the index of the item witch name is some_item_7.

var app = angular.module('myApp', []);

app.controller('MyCtrl', ['$scope',
  function MyCtrl($scope) {

    $scope.query = 'some';

    $scope.items = 
    [
      { name: 'some_item_1' },
      { name: 'another_item_2' },
      { name: 'some_item_3' },
      { name: 'another_item_4' },
      { name: 'some_item_5' },
      { name: 'another_item_6' },
      { name: 'some_item_7' },
      { name: 'another_item_8' },
      { name: 'some_item_9' }
    ];

    $scope.itemNext = function (item) {
      console.log(item.name);
    };

    $scope.getIndexFromName = function (name) {
      console.log("trying to get the index of the item with name = " + name);
    }

    $scope.getIndexFromName('some_item_7');

  }
]);

http://plnkr.co/edit/C8gL9qV1MyonTwDENO9L?p=$p$ PVIEW

任何想法?

推荐答案

您纳克重复前pression创建你的范围filteredList数组。结果
&LT;李NG重复=项(filteredItems =(项目|过滤器:查询))&GT;

Your ng-repeat expression creates the filteredList array on your scope.
<li ng-repeat="item in (filteredItems = (items | filter:query))">

您可以遍历它喜欢的任何阵列,检查匹配名称参数的项目。
$ scope.filteredItems

You can loop through it like any array, checking for the item matching the name parameter. $scope.filteredItems

下面是一个演示: http://plnkr.co/69nnbaZaulgX0odG7g7Y

请参阅此有关的职位:<一href=\"http://stackoverflow.com/questions/11721863/angularjs-how-to-get-an-ngrepeat-filtered-result-reference\">AngularJS - 如何让

See this related post: AngularJS - how to get an ngRepeat filtered result reference

更新结果
您的评论表明,你不希望等待NG重复创建筛选项目的数组。您可以使用 $过滤器服务页面加载之前轻松地初始化同一阵列。用途:

Update
Your comments indicate that you don't want to wait for ng-repeat to create the array of filtered items. You can use the $filter service to easily initialize the same array before the page loads. Use:

$scope.filteredItems = $filter('filter')($scope.items, {name: $scope.query}, false)

这样做不会与 NG-重复 DOM创建过程中节约了筛选结果相同的 filteredItems 阵列干扰

Doing so does not interfere with ng-repeat saving its filter results to the same filteredItems array during DOM creation.

下面是一个更新(和互动)演示 HTTP ://plnkr.co/NSvBz1yWvmeFgXITutZF

Here is an updated (and interactive) demo: http://plnkr.co/NSvBz1yWvmeFgXITutZF

这篇关于angularjs - 如何根据其属性之一的值在控制器得到一个项目的索引,在NG重复过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆