1

Is it possible in angular 2+ component get content of some tags? Something like this

import { Component } from '@angular/core';
@Component({
  selector: 'my-component',
  template: '<div>My component <p><ng-content from='forp'/></p><span><ng-content from='forspan'/></span></div>',
})
export class AppComponent {

}

Then we write

    <my-component>
      <forp>Content for p-tag</forp>
      <forspan>Content for span-tag</forspan>
    </my-component>

And it build as

    <div>
      My component 
      <p>Content for p-tag</p>
      <span>Content for span-tag</span>
    </div>

?

1

1 Answer 1

1

Yes is it possible. You can create a component and take HTML when you will use you component like this:

<my-comp>
  <some-comp></some-comp>
  <some-other-comp></some-other-comp>
</my-comp>

MyComponent html will look like this:

<div>
  // some html code
  <ng-content select="some-other-comp"></ng-content>
  <ng-content select="some-comp"></ng-content>
  // some html code
</div>

Hope it will help

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

2 Comments

Oh, yes, it helps! But I thought that I can do it without creating other components.
Yes but only one html will be inject in your component and ng-content will not have select attribute.

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.