I will just add one important thing that was skipped in the explanations above and explains when you MUST use ngOnInit.
If you are doing any manipulation of the component's DOM via e.g. ViewChildren, ContentChildren or ElementRef, your native elements will not be available during the constructor phase.
However, since ngOnInit happens once the component has been created and the checks (ngOnChanges) have been called you can access the DOM at this point.
export class App implements OnInit {
@ViewChild('myTemplate') myTemplate: TemplateRef<any>;
constructor(private elementRef: ElementRef) {
// this.elementRef.nativeElement is undefined here
// this.myTemplate is undefined here
}
ngOnInit() {
// this.elementRef.nativeElement can be used from here on
// this.myTemplate can be used from here on
}
}
Constructor is the first, and it happens sometimes when @input data is null!
so we use Constructor for declare services and ngOnInit happens after.
Exsample for contrutor:
ngOnInit(), ngOnChanges() and ngOnDestroy() etc. are lifecycle methods. ngOnChanges() will be the first to be called, before ngOnInit(), when the value of a bound property changes, it will NOT be called if there is no change. ngOnDestroy() is called when the component is removed. To use it, OnDestroy needs to be implemented by the class.
Both methods have different goals/responsibilities. The task of the constructor (which is a language supported feature) is to make sure that the representation invariant holds. Otherwise stated to make sure that the instance is valid by giving correct values to the members. It is up to the developer to decide what 'correct' means.
The task of the onInit() method (which is an angular concept) is to allow method invocations on a correct object (representation invariant). Each method should in turn make sure that the representation invariant holds when the method terminates.
The constructor should be used to create 'correct' objects, the onInit method gives you the opportunity to invoke method calls at a well defined instance.
To test this, I wrote this code, borrowing from the NativeScript Tutorial:
user.ts
login.component.ts
Console output
I will just add one important thing that was skipped in the explanations above and explains when you MUST use
ngOnInit
.If you are doing any manipulation of the component's DOM via e.g. ViewChildren, ContentChildren or ElementRef, your native elements will not be available during the constructor phase.
However, since
ngOnInit
happens once the component has been created and the checks (ngOnChanges
) have been called you can access the DOM at this point.Constructor is the first, and it happens sometimes when @input data is null! so we use Constructor for declare services and ngOnInit happens after. Exsample for contrutor:
Exsample for onInit:
I think that onInit is like InitialComponents() in winForm .
constructor()
is used to do dependency injection.ngOnInit()
,ngOnChanges()
andngOnDestroy()
etc. are lifecycle methods.ngOnChanges()
will be the first to be called, beforengOnInit()
, when the value of a bound property changes, it will NOT be called if there is no change.ngOnDestroy()
is called when the component is removed. To use it,OnDestroy
needs to beimplement
ed by the class.Both methods have different goals/responsibilities. The task of the constructor (which is a language supported feature) is to make sure that the representation invariant holds. Otherwise stated to make sure that the instance is valid by giving correct values to the members. It is up to the developer to decide what 'correct' means.
The task of the onInit() method (which is an angular concept) is to allow method invocations on a correct object (representation invariant). Each method should in turn make sure that the representation invariant holds when the method terminates.
The constructor should be used to create 'correct' objects, the onInit method gives you the opportunity to invoke method calls at a well defined instance.
constructor() is the default method in the Component life cycle and is used for dependency injection. Constructor is a Typescript Feature.
ngOnInit() is called after the constructor and ngOnInit is called after the first ngOnChanges.
i.e. Constructor()->ngOnChanges()->ngOnInit()
as mentioned above ngOnChanges() is called when an input or output binding value changes.