Skip to content

Commit

Permalink
fix(material/chips): error if empty getter is accessed too early (#27405
Browse files Browse the repository at this point in the history
)

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.

(cherry picked from commit c03b5f4)
  • Loading branch information
crisbeto committed Jul 4, 2023
1 parent 0184a4d commit 82c2700
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/material/chips/chip-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ describe('MDC-based MatChipSet', () => {

expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
});

it('should be able to access the `empty` getter before the chips are initialized', () => {
const fixture = TestBed.createComponent(BasicChipSet);
const chipSet = fixture.debugElement.query(By.directive(MatChipSet))!;
expect(chipSet.componentInstance.empty).toBe(true);
});
});

@Component({
Expand Down
2 changes: 1 addition & 1 deletion src/material/chips/chip-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class MatChipSet

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

/** The ARIA role applied to the chip set. */
Expand Down

0 comments on commit 82c2700

Please sign in to comment.