@@ -15,11 +15,13 @@ import {
1515 Inject ,
1616 Input ,
1717 NgZone ,
18+ OnChanges ,
1819 OnDestroy ,
1920 Optional ,
2021 Renderer2 ,
22+ SimpleChanges ,
2123} from '@angular/core' ;
22- import { ThemePalette , mixinDisabled , CanDisableCtor , CanDisable } from '@angular/material/core' ;
24+ import { CanDisable , CanDisableCtor , mixinDisabled , ThemePalette } from '@angular/material/core' ;
2325import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations' ;
2426
2527
@@ -53,7 +55,7 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
5355 '[class.mat-badge-disabled]' : 'disabled' ,
5456 } ,
5557} )
56- export class MatBadge extends _MatBadgeMixinBase implements OnDestroy , CanDisable {
58+ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy , OnChanges , CanDisable {
5759 /** Whether the badge has any content. */
5860 _hasContent = false ;
5961
@@ -81,14 +83,7 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
8183 @Input ( 'matBadgePosition' ) position : MatBadgePosition = 'above after' ;
8284
8385 /** 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 ;
9287
9388 /** Message used to describe the decorated element via aria-describedby */
9489 @Input ( 'matBadgeDescription' )
@@ -144,6 +139,18 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
144139 return this . position . indexOf ( 'before' ) === - 1 ;
145140 }
146141
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+
147154 ngOnDestroy ( ) {
148155 const badgeElement = this . _badgeElement ;
149156
0 commit comments