@@ -15,11 +15,13 @@ import {
15
15
Inject ,
16
16
Input ,
17
17
NgZone ,
18
+ OnChanges ,
18
19
OnDestroy ,
19
20
Optional ,
20
21
Renderer2 ,
22
+ SimpleChanges ,
21
23
} from '@angular/core' ;
22
- import { ThemePalette , mixinDisabled , CanDisableCtor , CanDisable } from '@angular/material/core' ;
24
+ import { CanDisable , CanDisableCtor , mixinDisabled , ThemePalette } from '@angular/material/core' ;
23
25
import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
24
26
25
27
@@ -53,7 +55,7 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
53
55
'[class.mat-badge-disabled]' : 'disabled' ,
54
56
} ,
55
57
} )
56
- export class MatBadge extends _MatBadgeMixinBase implements OnDestroy , CanDisable {
58
+ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy , OnChanges , CanDisable {
57
59
/** Whether the badge has any content. */
58
60
_hasContent = false ;
59
61
@@ -81,14 +83,7 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
81
83
@Input ( 'matBadgePosition' ) position : MatBadgePosition = 'above after' ;
82
84
83
85
/** The content for the badge */
84
- @Input ( 'matBadge' )
85
- get content ( ) : string { return this . _content ; }
86
- set content ( value : string ) {
87
- this . _content = value ;
88
- this . _hasContent = value != null && `${ value } ` . trim ( ) . length > 0 ;
89
- this . _updateTextContent ( ) ;
90
- }
91
- private _content : string ;
86
+ @Input ( 'matBadge' ) content : string ;
92
87
93
88
/** Message used to describe the decorated element via aria-describedby */
94
89
@Input ( 'matBadgeDescription' )
@@ -144,6 +139,18 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
144
139
return this . position . indexOf ( 'before' ) === - 1 ;
145
140
}
146
141
142
+ ngOnChanges ( changes : SimpleChanges ) {
143
+ const contentChange = changes . content ;
144
+
145
+ // Note that it's important that we do this in ngOnChanges, because with Ivy we aren't
146
+ // guaranteed for the DOM to be initialized when the `content` setter is run for the first time.
147
+ if ( contentChange ) {
148
+ const value = contentChange . currentValue ;
149
+ this . _hasContent = value != null && `${ value } ` . trim ( ) . length > 0 ;
150
+ this . _updateTextContent ( ) ;
151
+ }
152
+ }
153
+
147
154
ngOnDestroy ( ) {
148
155
const badgeElement = this . _badgeElement ;
149
156
0 commit comments