Angular 6多重ng内容

Luc*_*tos 35 angular-components angular angular6 ng-content

我正在尝试使用角度6中的多个ng内容构建自定义组件,但这不起作用,我不知道为什么.

这是我的组件代码:

<div class="header-css-class">
<ng-content select="#header"></ng-content>
</div>
 <div class="body-css-class">
<ng-content select="#body"></ng-content>
</div>
Run Code Online (Sandbox Code Playgroud)

我试图在另一个地方使用这个组件,并在body内部呈现两个不同的HTML代码,并在ng-content中选择标题,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

但该组件呈现为空白.你们知道我可能做错了什么,或者在同一个组件中渲染两个不同部分的最佳方法是什么?

谢谢!

Ami*_*ani 71

  1. 您可以添加虚拟属性header,body而不是模板引用(#header, #body).
  2. 而transclude使用ng-content具有select类似属性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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

DEMO

  • 如果你不想渲染额外的 div 或任何其他标签,你应该使用 &lt;ng-container&gt; (16认同)
  • @AmitChigadani 我相信@sobczi 的意思是你可以用“&lt;ng-container header&gt;”替换“&lt;div header&gt;”。 (7认同)
  • 我确认用“&lt;ng-container header&gt;”替换“&lt;div header&gt;”也有效。 (6认同)
  • 对于任何想要更多细节的人来说,这称为多插槽内容项目(而不是单插槽)。Angular 文档有一些很好的例子:https://angular.io/guide/content-projection (3认同)

Dom*_*nik 69

适合Web 组件规范。即使那是 Angular。这是关于避免选择器的属性,如 Angular 指令或其他用途的保留属性。所以,我们只使用“slot”属性。我们将看到<ng-content select="[slot=foobar]"><slot name="foobar">

例子:

hello-world.component.html

<ng-content select="[slot=start]"></ng-content>
<span>Hello World</span>
<ng-content select="[slot=end]"></ng-content>
Run Code Online (Sandbox Code Playgroud)

应用程序组件.html

<app-hello-world>
  <span slot="start">This is a </span>
  <span slot="end"> example.</span>
</app-hello-world>
Run Code Online (Sandbox Code Playgroud)

结果

This is a Hello World example.
Run Code Online (Sandbox Code Playgroud)

Stackblitz 示例

您可以使用任何您想要的名称,例如“香蕉”或“鱼”。但是“开始”和“结束”是在前后放置元素的良好约定。


小智 13

或者,您可以使用:

应用程序.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


小智 11

作为另一个选项,您可以将模板传递给子组件,然后您将受益于能够将值绑定到内容/模板

父组件html

<app-child
    [templateHeader]="header"
    [templateContent]="content">
</app-child>

<ng-template #header
     let-data="data"> < -- how you get dynamic data
     what ever you would like the header to say
     {{data}}
</ng-template>

<ng-template #content>
    what ever you would like the content to say or any other component
</ng-template>
Run Code Online (Sandbox Code Playgroud)

子组件 ts

export class ChildComponent {
    @Input() templateHeader: TemplateRef<any>;
    @Input() templateContent: TemplateRef<any>;
}
Run Code Online (Sandbox Code Playgroud)

子组件 html

<div class="header-css-class">
    <ng-container
        *ngTemplateOutlet="
        templateHeader;
        context: {   , < -- if you want to pass data to your template
            data: data
        }">
    </ng-container>
</div>
<div class="content-css-class">
    <ng-container *ngTemplateOutlet="templateContent">
    </ng-container>
</div>
Run Code Online (Sandbox Code Playgroud)

有关模板的更完整说明,请参阅这篇精彩文章https://indepth.dev/posts/1405/ngtemplateoutlet


Wes*_*ves 6

补充其他答案:

您也可以使用自定义标签(如<ion-card><ion-card-header>、 和<ion-card-content>)来实现。

应用程序.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

您会收到一条警告消息,但它会起作用。您可以禁止显示警告消息或使用已知标记,例如headerfooter。但是,如果您不喜欢这些方法中的任何一种,则应该使用其他解决方案之一。


Jul*_*ner 5

如果您只想“接受”多个组件,您可以使用:

<ng-content select="custom-component,a"></ng-content>
Run Code Online (Sandbox Code Playgroud)

这接受自定义组件的元素以及锚点 (a) 元素,并且不会更改顺序。