0

I would like to create an app-my-component component, which can be used in the following way:

<app-my-component>
    <div>Some element inside</div>
</app-my-component>

Then I would like to use that component/html element inside the app-my-component, like this:

<p>You provided this element as parameter:</p>

<ng-template [ngTemplateOutlet]="inputElement"></ng-template>

How is it possible? I was not able to find any documentation about this, but I once saw a code using this feature, so I hope it is still possible.


I added this to the MyComponent class:

@ContentChild(TemplateRef)
public inputElement: TemplateRef;

But the element does not appear.

3
  • What does your template look like? Should be an <ng-content> in there.. Commented Jul 14, 2020 at 14:21
  • DId you tried using content projection? Commented Jul 14, 2020 at 14:23
  • @Bozhinovski I've never heard of content projection. I will check it out! Commented Jul 14, 2020 at 14:24

1 Answer 1

2

In parent component, wrap all the projected element inside ng-template like this

<app-my-component>
    <ng-template>
      <div>Some element inside</div>
    </ng-template>
</app-my-component>

Working Example

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

Comments

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.