Skip to content

Commit c03b5f4

Browse files
authored
fix(material/chips): error if empty getter is accessed too early (#27405)
Fixes that the `empty` getter wasn't null checking the `chips` field and would throw an error if it is accessed before `ngAfterContentInit`. Fixes #27404.
1 parent 8e4a2ec commit c03b5f4

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/material/chips/chip-set.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ describe('MDC-based MatChipSet', () => {
9898

9999
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
100100
});
101+
102+
it('should be able to access the `empty` getter before the chips are initialized', () => {
103+
const fixture = TestBed.createComponent(BasicChipSet);
104+
const chipSet = fixture.debugElement.query(By.directive(MatChipSet))!;
105+
expect(chipSet.componentInstance.empty).toBe(true);
106+
});
101107
});
102108

103109
@Component({

src/material/chips/chip-set.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class MatChipSet
9898

9999
/** Whether the chip list contains chips or not. */
100100
get empty(): boolean {
101-
return this._chips.length === 0;
101+
return !this._chips || this._chips.length === 0;
102102
}
103103

104104
/** The ARIA role applied to the chip set. */

0 commit comments

Comments
 (0)