The problem (Angular 7)
I would like to be able to "modify" the template of an overridden component without rewriting the whole template meaning it would share the same html template as the component it overrides. The goal is to be able to customize a component and that any change on the overriden component template would be directly reflected in the overriding component.
Maybe we could say that I want to share part of the logic and template between two components.
Example of current implementation
@Component({
selector: 'component-a',
templateUrl: './component-a.html'
})
export class ComponentAComponent {
constructor() { }
}
@Component({
selector: 'component-a',
templateUrl: './component-a2.html'
})
export class ComponentA2Component extends ComponentAComponent {
specificStuff = 'I am additional stuff';
constructor() { }
}
component-a2.component.html
...
[ same as beginning of component-a.component.html]
...
<!-- Some specific stuff like an additional label, button, mat form field -->
<div>
{{ specificStuff }}
</div>
...
[ same as end of component-a.component.html]
...
As we can see, if I change component-a.html it will not affect componentA2.
The goal
The goal would be that both component use the same template but final (compiled) DOM would be different. I do not want to do this with ngIf.
So something like
@Component({
selector: 'component-a',
templateUrl: './component-a.html'
})
export class ComponentAComponent {
public specificStuff;
constructor() { }
}
@Component({
selector: 'component-a',
templateUrl: './component-a.html'
})
export class ComponentA2Component extends ComponentAComponent {
public name = 'World';
public specificStuff = '<div> Hello {{name}} </div>';
constructor() { }
}
component-a.component.html
<div>Yo</div>
<ng-template #specificStuff></ng-template>
<div>Bye</div>
Resulting template for componentA:
<div>Yo</div>
<ng-template #specificStuff></ng-template>
<div>Bye</div>
Resulting template for componentA2:
<div>Yo</div>
<ng-template #specificStuff><div> Hello {{name}} </div></ng-template>
<div>Bye</div>
What I've looked
innerHTML: would not work with angular interpolation, directives, etc.
ngComponentOutlet: would need the creation of another component and dealing with the context + I would like to avoid to include a layer with the component selector to avoid breaking the styling
ngTemplateOutlet: Template would still need to be passed somehow ? ( maybe this is what I need but not sure how to use it)