@@ -88,7 +88,7 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
88
88
@Input ( 'matBadgePosition' ) position : MatBadgePosition = 'above after' ;
89
89
90
90
/** The content for the badge */
91
- @Input ( 'matBadge' ) content : string ;
91
+ @Input ( 'matBadge' ) content : string | number | undefined | null ;
92
92
93
93
/** Message used to describe the decorated element via aria-describedby */
94
94
@Input ( 'matBadgeDescription' )
@@ -188,7 +188,7 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
188
188
if ( ! this . _badgeElement ) {
189
189
this . _badgeElement = this . _createBadgeElement ( ) ;
190
190
} else {
191
- this . _badgeElement . textContent = this . content ;
191
+ this . _badgeElement . textContent = this . _stringifyContent ( ) ;
192
192
}
193
193
return this . _badgeElement ;
194
194
}
@@ -203,7 +203,7 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
203
203
this . _clearExistingBadges ( contentClass ) ;
204
204
badgeElement . setAttribute ( 'id' , `mat-badge-content-${ this . _id } ` ) ;
205
205
badgeElement . classList . add ( contentClass ) ;
206
- badgeElement . textContent = this . content ;
206
+ badgeElement . textContent = this . _stringifyContent ( ) ;
207
207
208
208
if ( this . _animationMode === 'NoopAnimations' ) {
209
209
badgeElement . classList . add ( '_mat-animation-noopable' ) ;
@@ -246,11 +246,12 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
246
246
/** Adds css theme class given the color to the component host */
247
247
private _setColor ( colorPalette : ThemePalette ) {
248
248
if ( colorPalette !== this . _color ) {
249
+ const classList = this . _elementRef . nativeElement . classList ;
249
250
if ( this . _color ) {
250
- this . _elementRef . nativeElement . classList . remove ( `mat-badge-${ this . _color } ` ) ;
251
+ classList . remove ( `mat-badge-${ this . _color } ` ) ;
251
252
}
252
253
if ( colorPalette ) {
253
- this . _elementRef . nativeElement . classList . add ( `mat-badge-${ colorPalette } ` ) ;
254
+ classList . add ( `mat-badge-${ colorPalette } ` ) ;
254
255
}
255
256
}
256
257
}
@@ -270,6 +271,14 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
270
271
}
271
272
}
272
273
274
+ /** Gets the string representation of the badge content. */
275
+ private _stringifyContent ( ) : string {
276
+ // Convert null and undefined to an empty string which is consistent
277
+ // with how Angular handles them in inside template interpolations.
278
+ const content = this . content ;
279
+ return content == null ? '' : `${ content } ` ;
280
+ }
281
+
273
282
static ngAcceptInputType_disabled : BooleanInput ;
274
283
static ngAcceptInputType_hidden : BooleanInput ;
275
284
static ngAcceptInputType_overlap : BooleanInput ;
0 commit comments