Skip to content

Commit 7fe0559

Browse files
authored
Fix TypeError in IgxTextHighlightDirective when destroyed before ngAfterViewInit (#16615)
* Add null check to clearHighlight() and test for early destruction
1 parent 4a3c2ec commit 7fe0559

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ describe('IgxHighlight', () => {
308308

309309
expect(() => component.highlight.activateIfNecessary()).not.toThrowError();
310310
});
311+
312+
it('Should not throw error when destroyed before ngAfterViewInit completes', () => {
313+
// Create the component but do NOT call detectChanges()
314+
// This simulates the directive being destroyed before ngAfterViewInit is called
315+
const fix = TestBed.createComponent(HighlightLoremIpsumComponent);
316+
317+
// Destroy the component without initializing it
318+
expect(() => fix.destroy()).not.toThrowError();
319+
});
311320
});
312321

313322
@Component({

projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ export class IgxTextHighlightDirective implements AfterViewInit, AfterViewChecke
302302
public clearHighlight(): void {
303303
this.clearChildElements(false);
304304

305-
this._lastSearchInfo.searchText = '';
306-
this._lastSearchInfo.matchCount = 0;
305+
if (this._lastSearchInfo) {
306+
this._lastSearchInfo.searchText = '';
307+
this._lastSearchInfo.matchCount = 0;
308+
}
307309
}
308310

309311
/**

0 commit comments

Comments
 (0)