From c03b5f415f4cccf41f6a5138dce822205a800da9 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 4 Jul 2023 11:36:04 +0200 Subject: [PATCH] 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. --- src/material/chips/chip-set.spec.ts | 6 ++++++ src/material/chips/chip-set.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/material/chips/chip-set.spec.ts b/src/material/chips/chip-set.spec.ts index 358a59a60f55..a5e6363d034b 100644 --- a/src/material/chips/chip-set.spec.ts +++ b/src/material/chips/chip-set.spec.ts @@ -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({ diff --git a/src/material/chips/chip-set.ts b/src/material/chips/chip-set.ts index db012fd23bef..e85da22a55d0 100644 --- a/src/material/chips/chip-set.ts +++ b/src/material/chips/chip-set.ts @@ -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. */