33 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44 */
55
6- import { Platform , PlatformModule } from '@angular/cdk/platform' ;
6+ import { PlatformModule } from '@angular/cdk/platform' ;
77import {
8+ AfterViewInit ,
89 ChangeDetectionStrategy ,
910 ChangeDetectorRef ,
1011 Component ,
1112 ElementRef ,
1213 EventEmitter ,
1314 Input ,
14- NgZone ,
1515 OnChanges ,
1616 Output ,
1717 ViewChild ,
@@ -59,7 +59,7 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'avatar';
5959 changeDetection : ChangeDetectionStrategy . OnPush ,
6060 encapsulation : ViewEncapsulation . None
6161} )
62- export class NzAvatarComponent implements OnChanges {
62+ export class NzAvatarComponent implements OnChanges , AfterViewInit {
6363 static ngAcceptInputType_nzGap : NumberInput ;
6464
6565 readonly _nzModuleName : NzConfigKey = NZ_CONFIG_MODULE_NAME ;
@@ -86,9 +86,7 @@ export class NzAvatarComponent implements OnChanges {
8686 constructor (
8787 public nzConfigService : NzConfigService ,
8888 private elementRef : ElementRef ,
89- private cdr : ChangeDetectorRef ,
90- private platform : Platform ,
91- private ngZone : NgZone
89+ private cdr : ChangeDetectorRef
9290 ) { }
9391
9492 imgError ( $event : Event ) : void {
@@ -104,7 +102,7 @@ export class NzAvatarComponent implements OnChanges {
104102 }
105103 this . cdr . detectChanges ( ) ;
106104 this . setSizeStyle ( ) ;
107- this . notifyCalc ( ) ;
105+ this . calcStringSize ( ) ;
108106 }
109107 }
110108
@@ -114,15 +112,19 @@ export class NzAvatarComponent implements OnChanges {
114112 this . hasSrc = ! ! this . nzSrc ;
115113
116114 this . setSizeStyle ( ) ;
117- this . notifyCalc ( ) ;
115+ this . calcStringSize ( ) ;
116+ }
117+
118+ ngAfterViewInit ( ) : void {
119+ this . calcStringSize ( ) ;
118120 }
119121
120122 private calcStringSize ( ) : void {
121- if ( ! this . hasText ) {
123+ if ( ! this . hasText || ! this . textEl ) {
122124 return ;
123125 }
124126
125- const textEl = this . textEl ! . nativeElement ;
127+ const textEl = this . textEl . nativeElement ;
126128 const childrenWidth = textEl . offsetWidth ;
127129 const avatarWidth = this . el . getBoundingClientRect ( ) . width ;
128130 const offset = this . nzGap * 2 < avatarWidth ? this . nzGap * 2 : 8 ;
@@ -132,23 +134,13 @@ export class NzAvatarComponent implements OnChanges {
132134 textEl . style . lineHeight = this . customSize || '' ;
133135 }
134136
135- private notifyCalc ( ) : void {
136- // If use ngAfterViewChecked, always demands more computations, so......
137- if ( this . platform . isBrowser ) {
138- this . ngZone . runOutsideAngular ( ( ) => {
139- setTimeout ( ( ) => {
140- this . calcStringSize ( ) ;
141- } ) ;
142- } ) ;
143- }
144- }
145-
146137 private setSizeStyle ( ) : void {
147138 if ( typeof this . nzSize === 'number' ) {
148139 this . customSize = `${ this . nzSize } px` ;
149140 } else {
150141 this . customSize = null ;
151142 }
143+
152144 this . cdr . markForCheck ( ) ;
153145 }
154146}
0 commit comments