Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/material/badge/badge.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ComponentFixture, TestBed, fakeAsync} from '@angular/core/testing';
import {Component, DebugElement, ViewEncapsulation} from '@angular/core';
import {Component, DebugElement, ViewEncapsulation, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MatBadge, MatBadgeModule} from './index';
import {ThemePalette} from '@angular/material/core';
Expand Down Expand Up @@ -200,6 +200,11 @@ describe('MatBadge', () => {
expect(preExistingFixture.nativeElement.querySelectorAll('.mat-badge-content').length).toBe(2);
});

it('should expose the badge element', () => {
const badgeElement = badgeNativeElement.querySelector('.mat-badge-content')!;
expect(fixture.componentInstance.badgeInstance.getBadgeElement()).toBe(badgeElement);
});

});

/** Test component that contains a MatBadge. */
Expand All @@ -221,6 +226,7 @@ describe('MatBadge', () => {
`
})
class BadgeTestApp {
@ViewChild(MatBadge, {static: false}) badgeInstance: MatBadge;
badgeColor: ThemePalette;
badgeContent: string | number = '1';
badgeDirection = 'above after';
Expand Down
10 changes: 9 additions & 1 deletion src/material/badge/badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
/** Unique id for the badge */
_id: number = nextId++;

private _badgeElement: HTMLElement;
private _badgeElement: HTMLElement | undefined;

constructor(
private _ngZone: NgZone,
Expand Down Expand Up @@ -162,6 +162,14 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
}
}

/**
* Gets the element into which the badge's content is being rendered.
* Undefined if the element hasn't been created (e.g. if the badge doesn't have content).
*/
getBadgeElement(): HTMLElement | undefined {
return this._badgeElement;
}

/** Injects a span element into the DOM with the content. */
private _updateTextContent(): HTMLSpanElement {
if (!this._badgeElement) {
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/badge.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export declare class MatBadge extends _MatBadgeMixinBase implements OnDestroy, O
position: MatBadgePosition;
size: MatBadgeSize;
constructor(_ngZone: NgZone, _elementRef: ElementRef<HTMLElement>, _ariaDescriber: AriaDescriber, _renderer: Renderer2, _animationMode?: string | undefined);
getBadgeElement(): HTMLElement | undefined;
isAbove(): boolean;
isAfter(): boolean;
ngOnChanges(changes: SimpleChanges): void;
Expand Down