在指令属性的使用过滤器会导致死循环$消化周期 [英] Using filter on directive attribute causes infinite loop in $digest cycle

查看:201
本文介绍了在指令属性的使用过滤器会导致死循环$消化周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要使用过滤与分离范围指令的尝试:

 < div标签=p.tags | refTags>< / DIV>

原因在$一个无限循环消化周期:

当应用程序的模型变得不稳定,每个$消化周期触发状态变化和后续的$消化周期会出现此错误。角检测到这种情况和prevents从导致浏览器无响应的无限循环。

  .directive'标签', - >
    限制:'A'
    范围:
        标签:'='
    templateUrl:'谐音/ tags.html
.filterrefTags', - >
    (标签) - GT;
        ['滤波','值数组']

的谐音/ tags.html

 < UL>
    <李NG重复=标签中的标签> {{tag.tag}}< /李>
< / UL>

控制器p.tags

  p.tags = ['HTML5,CSS,JavaScript的,角JS,骨干JS,节点JS,上海社会科学院+指南针', Oragami','跑','猫粮','#catfood']

这是正常的行为?


  1. 可以过滤器不会对传递到指令的适用范围分离?
  2. 的值使用
  3. 对此有一个解决方法吗?我需要过滤数组值

  4. 是否与不同的设计?
  5. 另一种解决方案

解决方案

我不认为这是一个指导问题,而是一个过滤问题。滤波器的目标是获得数组作为输入,并返回基于某些规则和条件,其中,数组项具有相同的结构输入另一个阵列。

这导致在$消化循环的无限循环的原因是,在过滤器中,每个消化循环过滤器返回一个不同的对象,导致额外的循环。

我建议你通过返回同一阵列开始,即输入=输出。检查所有的作品如预期。之后,添加相关的条件。

An attempt to use a filter on a directive with isolate scope:

<div tags="p.tags | refTags"></div>

Causes an infinite loop in the $digest cycle:

This error occurs when the application's model becomes unstable and each $digest cycle triggers a state change and subsequent $digest cycle. Angular detects this situation and prevents an infinite loop from causing the browser to become unresponsive.

.directive 'tags', ->
    restrict: 'A'
    scope:
        tags: '='
    templateUrl: 'partials/tags.html'


.filter 'refTags', ->
    (tags) ->
        ['a filtered', 'array of values']

partials/tags.html

<ul>
    <li ng-repeat="tag in tags">{{tag.tag}}</li>
</ul>

p.tags in the controller

p.tags = ['HTML5', 'CSS', 'JavaScript', 'Angular JS', 'Backbone JS', 'Node JS', 'SASS + Compass', 'Oragami', 'Running', 'Cat Food', '#catfood']

Is this behavior normal?

  1. Can a filter not be use on a value passing in to the isolate scope of a directive?
  2. Is there a workaround for this? I need to filter the arrays values
  3. Is there another solution with a different design?

解决方案

I don't think it is a directive problem but a filter problem. The goal of filter is to get an array as input and return another array based on some rules and conditions, where array items have the same structure as input.

The reason that causes an infinite loop in the $digest cycle is that in a filter, each digest cycle filter returns a different object that causes an additional cycle.

I suggest you to start by returning the same array, i.e. input = output. Check if all works as expected. After that add the relevant condition.

这篇关于在指令属性的使用过滤器会导致死循环$消化周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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