简体   繁体   English

@Input对象上的角度变化检测

[英]Angular change detection on @Input object

I kind of understand how change detection works in Angular 2 but I'm really struggling to transfer/change my AngularJS methods over to NG2 regarding change detection. 我有点理解变量检测在Angular 2中是如何工作的,但我真的很难将我的AngularJS方法转移/更改为关于变化检测的NG2。

Imagine I have a component that takes a single @Input() anObject and has a single function, logAllProperties() which, for arguments sake, logs all the properties to the console. 想象一下,我有一个组件,它接受一个@Input()anObject并且只有一个函数logAllProperties() ,为了论证, 它将所有属性记录到控制台。 All I want to do is to call logAllProperties() every time anObject changes in anyway whatsoever. 我想做的就是每当 anObject以任何方式改变调用logAllProperties()

I understand that treating the object as immutable (either with an external library like Immutable.js or by enforcing that the entire object changes even for a minor property change) will trigger ngOnChange but is there anyway to call a function whenever a change takes place aside from these or using DoCheck which I understand is potentially extremely inefficient. 我理解将对象视为不可变(使用像Immutable.js这样的外部库或者通过强制整个对象即使对于较小的属性更改也会更改)将触发ngOnChange,但无论如何只要在一旁发生更改就调用一个函数从这些或使用DoCheck我理解可能是非常低效。

As well as "is there a way", what is the correct way to do something like this? 除了“有办法”之外,做这样的事情的正确方法是什么? I'm new to Angular2 so I'm more than happy to learn the right way if Immutable and Observables are the way to go. 我是Angular2的新手,所以我非常乐意学习正确的方法,如果不可变和可观察的方式。

Thanks in advance 提前致谢

I found out in our projects that using set works well in some cases, but others cases that need sync between components could be troubling, then we are using ngOnChange for those cases. 我在我们的项目中发现使用set在某些情况下运行良好,但是其他需要在组件之间同步的情况可能会令人不安,那么我们在这些情况下使用ngOnChange Here's an example. 这是一个例子。

@Input() set myProperty(myProperty: MyPropertyType) {
        if (myProperty) {
            this.logService.log(myProperty);
        }

        this._myProperty = myProperty;
}
private _myProperty: MyPropertyType= new MyPropertyType();

The thing about Angular2 new lifecycle still confuse, and the documentation samples doesn't fit most of the real world cases. 关于Angular2新生命周期的事情仍然令人困惑,文档样本不适合大多数现实世界的案例。

@input decorator is denoted your component is expecting to get some data from its parent which will be caught at that property @input decorator表示您的组件期望从其父级获取一些数据,这些数据将在该属性中捕获

same way @output is the decorator in angular2 wherein parent component you can catch something from child. 同样的方式@output@output中的装饰器,其中父组件可以从child中捕获一些东西。

Internally it uses observable and I don't think it's immutable. 在内部,它使用了可观察的,我不认为它是不可变的。

ngrx provides a redux type immutable mechanism where every state is remembered and you can go back to them as well. ngrx提供了一个redux类型的不可变机制,每个状态都被记住,你也可以回到它们。

Observable and ngrx are running in parallel now. Observable和ngrx现在并行运行。

The straightforward way of executing an action everytime anObject change is by using a setter or ngOnChange (see Fals' answer). 每次anObject更改时执行操作的直接方法是使用setter或ngOnChange(请参阅Fals的答案)。

Using immutable objects witch a change detection strategy of OnPush is a way to improve change detection performance. 使用不可变对象, OnPush的变化检测策略是一种提高变化检测性能的方法。 This is not the most obvious use of change detection in Angular. 这不是Angular中变化检测最明显的用法。

For a complete explanation I suggest you read this really good article and if you have some time (45 minutes) you can check this video by the author of the article here 对于一个完整的解释,我建议你阅读这真是一篇好文章 ,如果你有一段时间(45分钟),您可以通过文章的作者检查这个视频在这里

From there, you should have a good understanding of what's going on. 从那里,你应该很好地了解正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM