0

I know this is an old question but I cant find a simple answer and it seems so strange.

I have a father component with its own html and several component that extends this one.

I need something like this:

Father's HTML template

<p class="father"> somethings </p>
<child-component></child-component> <-- some kind of angular tag

class ChildComponent extends FatherComponent

<p> Im a Child </p>

Result rendering Child

<p class="father"> somethings </p>
<p> Im a Child </p>

Is this so hard to get?

UPDATE AS ASKED

@Component({
    selector: 'BaseComponent',
    templateUrl: 'BaseComponent.html',
})
export class BaseComponent implements OnInit 
{
...
}



@Component({
    selector      : 'ChildBaseComponent ',
    templateUrl   : 'ChildBaseComponent .html',
  })
export class ChildBaseComponent extends BaseComponent
{
   ....
}
3
  • Could you share the component's declaration ? Commented May 5, 2020 at 12:44
  • Updated above. It is so simple I was thinking it wasn't important. Commented May 5, 2020 at 13:10
  • Notice your selector, in order to reference a component you should use the selector. BTW, according to the naming convention it should be something like "child-base" like demonstrated in the example. Then in order to use it just call it <child-base></child-base> Commented May 5, 2020 at 14:05

1 Answer 1

1

I think what you're looking for here is content projection. You can find plenty of articles about this topic. I modified you examples slightly

@Component({
    selector: 'BaseComponent',
    template: `
               <p class="father"> somethings </p>
               <ng-content></ng-content>
               `,
})
export class BaseComponent implements OnInit 
{
...
}



@Component({
    selector      : 'ChildBaseComponent',
    templateUrl   : `
                      <BaseComponent><p> Im a Child </p></BaseComponent>`,
  })
export class ChildComponent
{
@ViewChild(BaseComponent) public baseComponent: BaseComponent
   
}

The thing is Angular doesn't inherit metadata of the parent class, it overrides it. There plenty of tools angular provides to deal with code sharing. You can always access your parent component from the child thanks to ViewChild decorator. And as the answer to your question - no, there is no such tag.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. Unfortunally I was hooping Angular had some sort of Partial View like others MVC instead of repeating the father's selector in every components extending it.
The framework is evolving, maybe it will be supported in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.