0

I have a page that has a number of directives. There are a number of directives in the header/navigation each with there own scope. There is also a ng-repeat of 25 items and each one of those creates a directive each with its own scope.

One of the directives includes a form that includes a custom filter to display form errors, it looks like this:

<span>{{ createProjectForm.name.$error | nagParseErrors }}</span>

Now the concern I have right now is that nagParseErrors is being executed about 33 times when anything in any scope changes even though this data createProjectForm.name is binded to (with ng-model) is only contained in the controller scope and the directive's scope containing the form (which is just being passed to the directive from the controller scope). I know it is related to the number of scopes (or directives) on the page because if I limit the ng-repeat from 25 items to 1, the filter is only called 9 times. This also happend for built-in filters (like json, and it even runs more times).

Is there something I might be doing wrong here or is this in fact how it should work in AngularJS?

BTW, I realize now that displaying the errors might be better off as a directive than a filter I am planning on going the directive route however I would like to clear up my understanding of filters here since I will probably run into this at some point down the road.

4

1 回答 1

0

这已被多次解决。所有 AngularJS 表达式将在应用程序的整个生命周期中不断地重新评估。这就是 AngularJS 中双向数据绑定的工作方式。

因此,您的代码没有任何问题。只是您需要确保您的过滤器是幂等的(在给定相同输入的情况下返回相同的输出)。

有关更多信息,请查看为什么 Scope.$apply() 调用 $rootScope.$digest() 而不是 this.$digest()?范围文档

于 2013-06-19T14:51:21.710 回答