Contenu connexe Similaire à Component lifecycle hooks in Angular 2.0 (20) Component lifecycle hooks in Angular 2.04. <!-- Expressions -->
Please type your name : {{name}}
<!-- Directives & Data Binding -->
Name: <input ng-model="name" value="..." />
Template
name :
Scope
value
elm.bind('keydown', … )
$scope.$watch('name', … )
Directive
$rootScope
8. tick(): void {
if (this._runningTick) {
throw new BaseException("ApplicationRef.tick is called recursively");
}
var s = ApplicationRef_._tickScope();
try {
this._runningTick = true;
this._changeDetectorRefs.forEach((detector) => detector.detectChanges());
if (this._enforceNoNewChanges) {
this._changeDetectorRefs
.forEach((detector) => detector.checkNoChanges());
}
} finally {
this._runningTick = false;
wtfLeave(s);
}
}
15. class Component{
data:any = {};
_differ: KeyValueDiffer;
constructor(private _differs:KeyValueDiffers){
this._differ = this._differs.find(this.data).create(null);
}
ngDoCheck() {
var changes = this._differ.diff(this.data);
if (changes) {
changes.forEachAddedItem ((r) => { log(r.key,r.currentValue); });
changes.forEachChangedItem((r) => { log(r.key,r.currentValue); });
changes.forEachRemovedItem((r) => { log(r.key,r.currentValue); });
}
}
}
16. class Component{
data:any = [];
_differ: IterableDiffer;
constructor(private _differs:IterableDiffers){
this._differ = this._differs.find(this.data).create(null);
}
ngDoCheck() {
var changes = this._differ.diff(this.data);
if (changes) {
changes.forEachAddedItem ((r) => { this.addData.push(r)});
changes.forEachRemovedItem((r) => { this.removeData.push(r)});
}
}
}
24. Hooks Descriptions
ngOnChanges Called when an input or output binding value changes
ngOnInit After the first ngOnChanges
ngDoCheck Developer's custom change detection
ngAfterContentInit After component content initialized
ngAfterContentChecked After every check of component content
ngAfterViewInit After component's view(s) are initialized
ngAfterViewChecked After every check of a component's view(s)
ngOnDestroy Just before the directive is destroyed