Angular @Input getter/setter and non-primitive values

你说的曾经没有我的故事 提交于 2019-12-05 09:10:44

There are two parts to this:

  1. Recognizing that the @Input decorator is only updated during change detection, therefore the setter assigned to the bound data will only fire during change detection. This fact is clearly stated in the first two comment lines in the Angular source code.

export interface InputDecorator { /** * Declares a data-bound input property. * * Angular automatically updates data-bound properties during change detection. *

  1. From 1, it then follows that we need to understand how Angulars change detection applies to non-primatives.

To help explain this I will use the following object ObjA:

public ObjA = {
    'prop1': 'value1',
    'prop2': 'value2'
  }

Angulars change detection fires when the value of the data bound property changes. However, when the property being bound to is an object like ObjA, it is a reference of ObjA that gets bound to, not the object itself. It is for this reason when a property value in ObjA changes ( a state change) Angulars change detection does not fire. Angular is not aware of the state of ObjA, but rather the reference to ObjA.

Thank you to @JBNizet and @Jota.Toledo for providing me the information (in the above comments) I needed to understand this topic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!