繁体   English   中英

列出(Angular.js)中的对象

[英]list an object in (Angular.js)

我正在生成一个列表来搜索键“名称”和“类型”。

results.push({ name: item.beast, type: 'color of animal' });

但我看到此错误,以查找数组$ scope.data中包含的元素:

Error: [$ rootScope: infdig] $ 10 digest () iterations reached. Aborting! Watchers fired in the last five iterations.

这是我的代码:

http://plnkr.co/edit/EDd578?p=preview

这里的问题是,您正在使用一组数据进行过滤,但尝试显示来自该过滤过程的结果数据集,格式不同。 我建议对输入使用ng-change并使用新的数据集来填充重复的项目。

调节器

$scope.matches = [];

$scope.findMatches = function(items, searchText) {
var results = [];
if (searchText) {
  angular.forEach(items, function(item) {
    if (item.beast.indexOf(searchText) === 0) {
      results.push({
        name: item.beast,
        type: 'animal'
      });
    }

    if (item.color.indexOf(searchText) === 0) {
      results.push({
        name: item.color,
        type: 'color of animal'
      });
    }
  });
}

return results;
}

HTML

<input type='text' ng-model='search' id='search' ng-change="matches = findMatches(data, search)">
<hr/>

<ul>
  <li ng-repeat="item in matches track by $index">{{item.name}} and {{item.type}}</li>
</ul>

plunkr- http: //plnkr.co/edit/hkMXPP?p=preview

每次运行过滤器时,您都在创建一个新数组,然后将其返回。 这使angular认为您每次都更改了数组(它不会检查项目是否相等,而是通过===检查引用是否相等)。

看看这个以获得更多细节。

一种解决方案是就地修改items数组,然后将其返回,因此引用保持不变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM