diff --git a/projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.spec.ts b/projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.spec.ts index 98e10d64095..32c0e855f0b 100644 --- a/projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.spec.ts +++ b/projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.spec.ts @@ -308,6 +308,15 @@ describe('IgxHighlight', () => { expect(() => component.highlight.activateIfNecessary()).not.toThrowError(); }); + + it('Should not throw error when destroyed before ngAfterViewInit completes', () => { + // Create the component but do NOT call detectChanges() + // This simulates the directive being destroyed before ngAfterViewInit is called + const fix = TestBed.createComponent(HighlightLoremIpsumComponent); + + // Destroy the component without initializing it + expect(() => fix.destroy()).not.toThrowError(); + }); }); @Component({ diff --git a/projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.ts b/projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.ts index d1c8f7d226b..23f5b7bc6d7 100644 --- a/projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.ts +++ b/projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.ts @@ -302,8 +302,10 @@ export class IgxTextHighlightDirective implements AfterViewInit, AfterViewChecke public clearHighlight(): void { this.clearChildElements(false); - this._lastSearchInfo.searchText = ''; - this._lastSearchInfo.matchCount = 0; + if (this._lastSearchInfo) { + this._lastSearchInfo.searchText = ''; + this._lastSearchInfo.matchCount = 0; + } } /**