什么意味着“观察者反应函数不应改变模型”使用嵌套的ng-repeat? [英] What means "Observer reaction functions should not change model" using a nested ng-repeat?

查看:126
本文介绍了什么意味着“观察者反应函数不应改变模型”使用嵌套的ng-repeat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个简化版本的代码,其中我得到一个记录的错误,即使生成的HTML看起来不错。这个异常是什么意思?为什么我会收到?

Here's a simplified version of the code where I get an error that is logged even if the generated HTML looks good. What does this exception means ? Why do I get it ?

index.html

<!DOCTYPE html>
<div ng-app main>
  <div ng-repeat="a in ctrl.listOfA">
    <strong>{{ a }}</strong>
    <div ng-repeat="b in ctrl.listOfB(a)">
      {{ b }}
    </div>
  </div>
</div>

<script type="application/dart" src="main.dart"></script>
<script src="packages/browser/dart.js"></script>

main.dart

import 'package:angular/angular.dart';  /* 0.9.10 */

@NgController(selector: '[main]', publishAs: 'ctrl')
class MainController {
  final listOfA = new List.generate(2, (i) => 'a$i');
  listOfB(a) => new List.generate(2, (i) => 'b$i').map((e) => '$a-$e');
}

void main() {
  ngBootstrap(module: new Module()
    ..type(MainController)
  );
}

记录的错误:

Observer reaction functions should not change model. 
These watch changes were detected: ctrl.listOfB(a): (a0-b0, a0-b1) <= (a0-b0, a0-b1); ctrl.listOfB(a): (a1-b0, a1-b1) <= (a1-b0, a1-b1)
These observe changes were detected: 

STACKTRACE:
#0      RootScope.flush.<anonymous closure> (package:angular/core/scope.dart:554:11)
#1      RootScope.flush (package:angular/core/scope.dart:560:9)
#2      RootScope.flush (package:angular/core/scope.dart:561:7)
#3      RootScope.flush (package:angular/core/scope.dart:561:7)
#4      apply (package:angular/core/scope.dart:262:18)
#5      _rootRun (dart:async/zone.dart:710)
#6      _rootRun (dart:async/zone.dart:711)
#7      _rootRun (dart:async/zone.dart:711)
#8      _ZoneDelegate.run (dart:async/zone.dart:440)
#9      NgZone._finishTurn (package:angular/core/zone.dart:96:21)
#10     NgZone._onRunBase (package:angular/core/zone.dart:61:43)
#11     _onRun (package:angular/core/zone.dart:66:15)
#12     _ZoneDelegate.run (dart:async/zone.dart:440)
#13     _CustomizedZone.run (dart:async/zone.dart:650)
#14     NgZone.run (package:angular/core/zone.dart:148:35)
#15     ngBootstrap (package:angular/bootstrap.dart:92:18)
#16     main (http://127.0.0.1:3030/xxxx/web/test/main.dart:13:14)


推荐答案

Angular只支持表达式中的纯函数。我想这开始与0.9.10,因为最近已经有一个类似的问题,它更新后停止工作。

Angular only supports pure functions in expressions. I guess this started with 0.9.10 because there already was a similar question recently where it stopped working after the update.

ctrl.listOfB(a)

应始终返回相同参数的相同结果,

should always return the same result for the same argument,

或者即使结果相同,原因可能是 ctrl.listOfB(a) always返回相同结果的新实例。 Angular 0.9.10也不喜欢这样。

or even if the result is the same, the cause could be that ctrl.listOfB(a) always returns a new instance of the same result. Angular 0.9.10 doesn't like this either.

它会重复计算表达式,并且每次抛出时都会收到一个新的结果。

It repeatedly evaluates the expression and if it receives a new result each time it throws.

我认为解决方案是缓存结果并从缓存返回,如果已经可用。

I think the solution is to cache the results and return from the cache if already available.

这篇关于什么意味着“观察者反应函数不应改变模型”使用嵌套的ng-repeat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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