[英]What is the difference between Angular ngOnInit() and ngOnChanges()?
Angular provides lifecycle hook ngOnInit() and ngOnChanges() by default. Angular 默认提供生命周期钩子 ngOnInit() 和 ngOnChanges()。 Why should ngOnInit be used, if we already have a ngOnChanges?如果我们已经有了 ngOnChanges,为什么要使用 ngOnInit? And constructor also.而构造器也。
To keep it very short.保持非常简短。
ngOnInit()
is used to execute any piece of code for only one time (for eg : data fetch on load). ngOnInit()
用于仅执行任何一段代码一次(例如:加载时获取数据)。
ngOnChanges()
will execute on every @Input()
property change. ngOnChanges()
将在每次@Input()
属性更改时执行。
If you want to execute any component method, based on the @Input()
value change, then you should write such logic inside ngOnChanges()
.如果你想根据@Input()
值的变化执行任何组件方法,那么你应该在ngOnChanges()
编写这样的逻辑。
As you claim why do we need ngOnInit()
when we have ngOnChanges(),
it is because you cannot execute one time code, on every @Input()
property change.正如您声称为什么当我们有ngOnInit()
时我们需要ngOnChanges(),
这是因为您无法在每次@Input()
属性更改时执行一次代码。 And you cannot use constructor
as the replacement of ngOnInit()
as well.并且您也不能使用constructor
来替代ngOnInit()
。 Because the bindings, such as @Input properties are not available within the constructor.因为绑定(例如 @Input 属性)在构造函数中不可用。
I think you will get fair idea with this Diff between OnInit and constructor我认为你会对OnInit 和构造函数之间的这个Diff有一个公平的想法
How a form need be setup如何设置表单
0. Static design Html markup should hold how the design is structured and laid out. 0. 静态设计Html 标记应该包含设计的结构和布局方式。 Any permanent classes are to be applied directly in markup.任何永久类都将直接应用于标记中。
1. Constructor 1. 构造函数
Setup dependencies, like services, providers, configuration etc. These enable the component to manage itself along with interact with other elements.设置依赖项,如服务、提供者、配置等。这些使组件能够管理自身以及与其他元素交互。
2. Initializer (ngOnInit) 2. 初始化器(ngOnInit)
Populates form elements like dropdowns etc when their values are to be retrieved from external source, rather than being known at design time.当要从外部源检索它们的值而不是在设计时已知时,填充表单元素,例如下拉列表等。 This is to be done once only to setup the initial rendering of the form这仅用于设置表单的初始渲染
3. Input changes (ngOnChanges) 3. 输入变化(ngOnChanges)
Runs on every change on any input, and perform any action which gets triggered by that particular control.在任何输入的每个更改上运行,并执行由该特定控件触发的任何操作。 For example, if there are multiple inputs and on any validation failure on a single one, you need to focus on the failed control and disable all others, you can do it here.例如,如果有多个输入并且单个输入的任何验证失败,您需要关注失败的控件并禁用所有其他控件,您可以在此处执行此操作。 Useful for validation logic.对验证逻辑很有用。
Not to be used to handle other control's layout and structure.不用于处理其他控件的布局和结构。
This often runs recursively if one control impacts others so logic has to be carefully designed.如果一个控件影响其他控件,这通常会递归运行,因此必须仔细设计逻辑。
If you want to prevent this from running, you can disable the Angular change detection and manually handle the state yourself.如果你想阻止它运行,你可以禁用 Angular 更改检测并自己手动处理状态。
4. Control's event handlers Here you take in the final value of the control and use it to perform manipulation of other controls in the form. 4. 控件的事件处理程序在此获取控件的最终值并使用它来执行表单中其他控件的操作。 As soon as you change the value of other controls, the ngOnChanges event fires again.一旦你改变了其他控件的值,ngOnChanges 事件就会再次触发。
ngOnChanges() is called whenever input bound properties of its component changes, it receives an object called SimpleChanges which contains changed and previous property.每当其组件的输入绑定属性发生更改时,都会调用 ngOnChanges(),它会接收一个名为 SimpleChanges 的对象,该对象包含已更改的和先前的属性。
ngOnInit() is used to initialize things in a component,unlike ngOnChanges() it is called only once and after first ngOnChanges(). ngOnInit() 用于初始化组件中的内容,与 ngOnChanges() 不同,它只调用一次,并且在第一个 ngOnChanges() 之后调用。
ngOnChanges will be called first on the life cycle hook when there is a change to the component inputs through the parent.当通过父级对组件输入进行更改时,将首先在生命周期挂钩上调用 ngOnChanges。
ngOnInit will be called only once on initializing the component after the first ngOnChanges called.在第一个 ngOnChanges 调用之后, ngOnInit 只会在初始化组件时被调用一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.