在AngularJS中,您可以使用$scope
的$watch
函数指定观察者来观察作用域变量的变化.什么等同于观察Angular 中的变量更改(例如,在组件变量中)?
在AngularJS中,您可以使用$scope
的$watch
函数指定观察者来观察作用域变量的变化.什么等同于观察Angular 中的变量更改(例如,在组件变量中)?
在Angular 2中,变化检测是自动的...$scope.$watch()
和$scope.$digest()
卢比.
不幸的是,开发指南的变更检测部分还没有编写(在Architecture Overview页的底部"其他内容"部分有一个占位符).
以下是我对变化检测的工作原理的理解:
setTimeout()
而不是像$timeout
这样的东西...因为setTimeout()
是猴子补的.ChangeDetectorRef
来访问此对象.)这些变化检测器是在Angular创建组件时创建的.它们会跟踪所有绑定的状态,以便进行脏判断.从某种意义上说,它们类似于Angular 1为{{}}
个模板绑定设置的自动$watches()
onPush
change detection strategy on any of your components), every component in the tree is examined once (TTL=1)... from the top, in depth-first order. (Well, if you're in dev mode, change detection runs twice (TTL=2). See ApplicationRef.tick() for more about this.) It performs dirty checking on all of your bindings, using those change detector objects.
ngOnChanges()
to be notified of changes. ngDoCheck()
(see this SO answer for more on this). 了解更多信息的其他参考资料:
onPush
.