Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在(Angular.js)中列出一个对象_Javascript_Angularjs - Fatal编程技术网

Javascript 在(Angular.js)中列出一个对象

Javascript 在(Angular.js)中列出一个对象,javascript,angularjs,Javascript,Angularjs,我正在生成一个列表来搜索关键字“name”和“type” 但是,我看到了这个错误,无法找到数组$scope.data中包含的元素: Error: [$ rootScope: infdig] $ 10 digest () iterations reached. Aborting! Watchers fired in the last five iterations. 这是我的代码: 每次运行过滤器时,您都会创建一个新数组,并返回该数组。这使angular认为您每次都更改了数组(它不检查项是否相等

我正在生成一个列表来搜索关键字“name”和“type”

但是,我看到了这个错误,无法找到数组$scope.data中包含的元素:

Error: [$ rootScope: infdig] $ 10 digest () iterations reached. Aborting! Watchers fired in the last five iterations.
这是我的代码:


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

请查看以了解更多详细信息


一种解决方案是在原地修改items数组并返回它,这样引用就保持不变。

这里的问题是,您使用的是一组数据进行筛选,但试图以不同的格式显示筛选过程产生的数据集。我主张对输入使用
ng change
,并使用新的数据集来填充重复项

控制器 html


  • {{item.name}和{{item.type}

plunkr-

这基本上是指:不知何故,您触发了一个潜在的无限循环,并且由于某种原因,角度检测范围的周期不断更新。(现在查看你的代码,希望我能为你找到更好的答案)我不知道该怎么办是的,我也不知道。我理解你为什么会遇到这个问题。这是因为每次循环一个项目时都要创建一个新数组。。。。希望我能找到一个解决办法:在多次尝试使用过滤器并像你们一样创建一个新的数组结构之后。没有办法让它与过滤器一起工作(有趣的想法tho)。这个想法太离经叛道了,这是不受支持的。一般来说,我建议你按照@jusopi的回答去做,因为过滤器遵循的原则是“它意味着过滤掉(删除)数组中不匹配的项,但不要将数组定制成与原始数组不同的东西”。我可以给出更多关于“为什么它不能与angular一起工作”的解释,但它与$digest和$scope生命周期等相关。这对于新手来说是非常复杂的。所以只要遵循我上面提到的原则,避免强迫过滤器做一些非常规的事情。谢谢,但我们已经失去了我所拥有的东西的本质。如果你看,我需要返回“beast”和“color”属性。我需要返回一个只有这两个属性的对象。如果我写“r”必须出现:rat,则重新选择语句“您需要修改模板以修复缺少的道具,因为现在它正在处理实际项目而不是新的匹配项目。”如果我写“r”,则应以“beast”和“com”颜色列出所有属性。在我的例子中是老鼠和红色。在我上传的例子中,它是如何工作的?很明显,最终目标是找到匹配的项目。这并不是什么新鲜事,但您显示内容的方式不是您的标准方法。如果你愿意为你的比赛使用现有的项目,并可能以不同的方式显示,你可能会更容易找到解决方案。现在,无论我如何使用matches创建设置,我都会遇到与您相同的$digest错误。哈哈,我有麻烦了。我尝试了很多事情,但我得到了解决办法
Error: [$ rootScope: infdig] $ 10 digest () iterations reached. Aborting! Watchers fired in the last five iterations.
$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;
}
<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>