Skip to content

Commit ec72a8d

Browse files
committed
added hook ngAfterContentChecked
1 parent 3a019ce commit ec72a8d

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
1-
import { Component, OnInit, OnChanges, Input, Output, EventEmitter } from '@angular/core';
1+
import { Component, OnInit, OnChanges, Input, Output, EventEmitter, SimpleChanges,
2+
DoCheck, AfterContentInit, AfterContentChecked } from '@angular/core';
23

34
@Component({
45
selector: 'app-product-details',
56
templateUrl: './product-details.component.html',
67
styleUrls: ['./product-details.component.css']
78
})
8-
export class ProductDetailsComponent implements OnInit, OnChanges {
9+
export class ProductDetailsComponent implements OnInit, OnChanges, DoCheck,AfterContentInit, AfterContentChecked {
910

1011
constructor() { }
1112
@Input() SalesRating:number = 3.5;
1213
@Output() GetLocalSales: EventEmitter<string> = new EventEmitter<string>();
1314
_salesRating: string = 'Default';
1415

1516
ngOnInit(): void {
17+
this._salesRating = 'Default';
18+
console.log("2. ngOnInit called from child.");
1619
}
1720

21+
ngDoCheck(): void {
22+
console.log("3. do check is called from child .");
23+
}
24+
25+
ngAfterContentInit(): void{
26+
console.log("4 .after content init from child");
27+
}
28+
29+
ngAfterContentChecked(): void{
30+
console.log("5. ngAfterContentChecked from child.");
31+
}
32+
1833
onClick():void{
1934
this.GetLocalSales.emit(`The local sales report for this product is ${this._salesRating}`);
20-
}
35+
}
2136

22-
ngOnChanges() : void{
37+
ngOnChanges(changes: SimpleChanges) : void{
2338
if(this.SalesRating == 3.5){
2439
this._salesRating = 'Good';
2540
} else if (this.SalesRating == 4.0){
@@ -29,8 +44,14 @@ export class ProductDetailsComponent implements OnInit, OnChanges {
2944
} else {
3045
this._salesRating = 'Undefined'
3146
}
32-
}
3347

34-
48+
for (const propName in changes) {
49+
const chng = changes[propName];
50+
const cur = JSON.stringify(chng.currentValue);
51+
const prev = JSON.stringify(chng.previousValue);
52+
//console.log(`${propName}: currentValue = ${cur}, previousValue = ${prev}`);
53+
}
3554

55+
console.log("1. ngOnChanges called from child.");
56+
}
3657
}

src/app/products/products.component.ts

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit,SimpleChanges, OnChanges, DoCheck,
2+
AfterContentInit, AfterContentChecked } from '@angular/core';
23

34
@Component({
45
selector: 'app-products',
56
templateUrl: './products.component.html',
67
styleUrls: ['./products.component.css']
78
})
8-
export class ProductsComponent implements OnInit {
9+
10+
export class ProductsComponent implements OnInit, OnChanges, DoCheck,AfterContentInit, AfterContentChecked {
911

1012
constructor() { }
1113

1214
ngOnInit(): void {
15+
console.log("2. ngOnInit called from parent.");
1316
}
1417

18+
ngOnChanges(changes: SimpleChanges) : void{
19+
for (const propName in changes) {
20+
const chng = changes[propName];
21+
const cur = JSON.stringify(chng.currentValue);
22+
const prev = JSON.stringify(chng.previousValue);
23+
console.log(`${propName}: currentValue = ${cur}, previousValue = ${prev}`);
24+
}
25+
console.log("1. ngOnChanges called from parent.");
26+
}
27+
28+
ngDoCheck(): void {
29+
console.log("3. doCheck is called from parent.") ;
30+
}
31+
32+
ngAfterContentInit(): void{
33+
console.log("4. afterCcontent init from parent.");
34+
}
35+
36+
ngAfterContentChecked(): void{
37+
console.log("5. ngAfterContentChecked from parent.");
38+
}
39+
1540
messageFromNestedComponent = '';
1641
onGetLocalSalesReport(message:string) : void {
1742
this.messageFromNestedComponent = 'Product Details Report: ' + message;
1843
this.products[0].AvilableQty = 1909;
1944
}
20-
21-
EditProduct()
22-
{
23-
alert('asde');
24-
}
2545

2646
products : any[] =[ {
2747
"ProductID" :1,

0 commit comments

Comments
 (0)