Multiple ng-content

后端 未结 4 446
庸人自扰
庸人自扰 2020-12-12 16:44

I am trying to build a custom component using multiple ng-content in Angular 6, but this is not working and I have no idea why.

This is my component code

相关标签:
4条回答
  • 2020-12-12 17:26

    alternatively you can use:

    app.comp.html

    <app-child>
        <div role="header">This should be rendered in header selection of ng-content</div>
        <div role="body">This should be rendered in body selection of ng-content</div>
    </app-child>
    

    child.comp.html

    <div class="header-css-class">
        <ng-content select="div[role=header]"></ng-content>
    </div>
    <div class="body-css-class">
        <ng-content select="div[role=body]"></ng-content>
    </div>
    
    0 讨论(0)
  • 2020-12-12 17:35
    1. You could add dummy attributes header and body as opposed to template references (#header, #body).
    2. And transclude using ng-content with select attribute like select="[header]".

    app.comp.html

    <app-child>
        <div header >This should be rendered in header selection of ng-content</div>
        <div body >This should be rendered in body selection of ng-content</div>
    </app-child>
    

    child.comp.html

    <div class="header-css-class">
        <ng-content select="[header]"></ng-content>
    </div>
    <div class="body-css-class">
        <ng-content select="[body]"></ng-content>
    </div>
    

    DEMO

    0 讨论(0)
  • 2020-12-12 17:41

    Complementing the other answers:

    You can also do it with custom tags (like <ion-card>, <ion-card-header>, and <ion-card-content>).

    app.comp.html

    <app-child>
        <app-child-header>This should be rendered in header selection of ng-content</app-child-header>
        <app-child-content>This should be rendered in content selection of ng-content</app-child-content>
    </app-child>
    

    child.comp.html

    <div class="header-css-class">
        <ng-content select="app-child-header"></ng-content>
    </div>
    <div class="content-css-class">
        <ng-content select="app-child-content"></ng-content>
    </div>
    

    You'll get a warning message, but it will work. You can suppress the warning messages or use known tags such as header or footer. However, if you don't like any of these methods, you should go with one of the other solutions.

    0 讨论(0)
  • 2020-12-12 17:43

    To fit the Web Component specs. Even if that's Angular. It's about avoiding attributes for selector like Angular directives or reserved attributes with another use. So, we just use the "slot" attribute. We'll see <ng-content select="[slot=foobar]"> as <slot name="foobar">.

    Example:

    hello-world.component.html

    <ng-content select="[slot=start]"></ng-content>
    <span>Hello World</span>
    <ng-content select="[slot=end]"></ng-content>
    

    app.component.html

    <app-hello-world>
      <span slot="start">This is a </span>
      <span slot="end"> example.</span>
    </app-hello-world>
    

    Result

    This is a Hello World example.
    

    Stackblitz Example

    You can use any name you want like "banana" or "fish". But "start" and "end" are a good convention to place elements before and after.

    0 讨论(0)
提交回复
热议问题