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}
0 commit comments