I am currently working on an Angular project that involves multiple forms. While these forms share a similar structure and functionality, there are some distinct differences between them in terms of both functionality and template design.
I am looking for a more efficient way to handle component inheritance, where I can define a base component with its functionality and template, but also have the flexibility to customize certain elements within the template in child components.
After some research, I came across a method involving extending the base component and dynamically modifying the parent element within the child component's template. I have provided a code example on SandBox (https://codesandbox.io/s/github/dsavke/inheratance-test) as a demonstration of this concept.
Here is the code snippet for the BaseFormComponent:
@Component({
selector: 'base-form',
templateUrl: './base-form.component.html'
})
export class BaseFormComponent {
private counter: number = 0;
@ContentChild('content') content!: TemplateRef<any>;
public editUrl: string = '';
public onEdit(): void {
//some logic
}
public onSave(): string {
if (!this.canSave())
return 'Not saved';
return 'Saved';
}
public canSave(): boolean {
return true || true;
}
private updateCounter(): void {
this.counter++;
}
}
As well as the template code for the BaseFormComponent:
<h2>Base component</h2>
<p>Can save form? {{canSave()}}</p>
<ng-container
style="border: 1px solid red;"
*ngIf="content"
[ngTemplateOutlet]="content"
></ng-container>
For the UserFormComponent:
@Component({
selector: 'user-form',
templateUrl: './user-form.component.html'
})
export class UserFormComponent extends BaseFormComponent {
public override canSave(): boolean {
return false;
}
}
UserFormComponent template code:
<base-form>
<ng-template #content>
<h5>This is from USER FORM!</h5>
</ng-template>
</base-form>
While this method of component inheritance works in Angular, it does come with some challenges:
- Duplicating instances of the BaseFormComponent occurs, one for the extension in UserFormComponent and another one for displaying the UI by adding the BaseFormComponent element in the template;
- Overriding methods in UserFormComponent may appear futile as the instance of the parent component (the extended component) remains hidden due to decorators. There is a need for two templates - one for the base component display and one for customizations in the child component using ng-template;
Is there an alternative approach to achieve inheritance, where both the template and functionality from the parent component are retained while allowing customization in the child component? This would represent true inheritance - is such a scenario feasible in Angular?
EDIT: The form in question is utilized in over 1000 locations, making it a significant part of the project. It consists of numerous child components and allows for UI customization through parameters. While the form inputs require manual adjustments, generic features are consistently used. The challenge lies in efficiently managing template modifications within this complex structure.