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' ;
2
3
3
4
@Component ( {
4
5
selector : 'app-product-details' ,
5
6
templateUrl : './product-details.component.html' ,
6
7
styleUrls : [ './product-details.component.css' ]
7
8
} )
8
- export class ProductDetailsComponent implements OnInit , OnChanges {
9
+ export class ProductDetailsComponent implements OnInit , OnChanges , DoCheck , AfterContentInit , AfterContentChecked {
9
10
10
11
constructor ( ) { }
11
12
@Input ( ) SalesRating :number = 3.5 ;
12
13
@Output ( ) GetLocalSales : EventEmitter < string > = new EventEmitter < string > ( ) ;
13
14
_salesRating : string = 'Default' ;
14
15
15
16
ngOnInit ( ) : void {
17
+ this . _salesRating = 'Default' ;
18
+ console . log ( "2. ngOnInit called from child." ) ;
16
19
}
17
20
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
+
18
33
onClick ( ) :void {
19
34
this . GetLocalSales . emit ( `The local sales report for this product is ${ this . _salesRating } ` ) ;
20
- }
35
+ }
21
36
22
- ngOnChanges ( ) : void {
37
+ ngOnChanges ( changes : SimpleChanges ) : void {
23
38
if ( this . SalesRating == 3.5 ) {
24
39
this . _salesRating = 'Good' ;
25
40
} else if ( this . SalesRating == 4.0 ) {
@@ -29,8 +44,14 @@ export class ProductDetailsComponent implements OnInit, OnChanges {
29
44
} else {
30
45
this . _salesRating = 'Undefined'
31
46
}
32
- }
33
47
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
+ }
35
54
55
+ console . log ( "1. ngOnChanges called from child." ) ;
56
+ }
36
57
}
0 commit comments