Skip to main content

Posts

Showing posts with the label Angular 17 Overriding LifeCycle methods

Angular 17 Overriding LifeCycle methods | Overriding vs Overloading

Overriding lifecycle methods: If a base class defines a lifecycle method, such as ngOnInit, a child class that also implements ngOnInit overrides the base class's implementation. If you want to preserve the base class's lifecycle method, explicitly call the method with super : @Component({ ... }) export class ListboxBase {   protected isInitialized = false;   ngOnInit() {     this.isInitialized = true;   } } @Component({ ... }) export class CustomListbox extends ListboxBase {   override ngOnInit() {     super.ngOnInit();     /* ... */   } } If a base class relies on dependency injection, the child class must explicitly pass these dependencies to super. @Component({ ... }) export class ListboxBase {   constructor(private element: ElementRef) { } } @Component({ ... }) export class CustomListbox extends ListboxBase {   constructor(element: ElementRef) {  ...