角6倍ng含量

时间:2018-10-04 03:49:01

标签: angular angular6 angular-components ng-content

我正在尝试使用angular 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>

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

<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>

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

谢谢!

6 个答案:

答案 0 :(得分:51)

要符合Web Component规范。即使那是Angular。这是关于避免将诸如Angular指令之类的选择器属性或保留属性用于其他用途。因此,我们只使用“插槽”属性。我们将<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>

app.component.html

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

结果

This is a Hello World example.

Stackblitz Example

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

答案 1 :(得分:40)

  1. 您可以添加虚拟属性headerbody,而不是模板引用(#header, #body)
  2. 然后将ng-contentselect这样的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

答案 2 :(得分:4)

补充其他答案:

您还可以使用自定义标签(例如<ion-card><ion-card-header><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>

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

答案 3 :(得分:2)

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

父组件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>
子组件 ts
export class ChildComponent {
    @Input() templateHeader: TemplateRef<any>;
    @Input() templateContent: TemplateRef<any>;
}
子组件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>

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

答案 4 :(得分:0)

或者您可以使用:

app.comp.html

Select count(*) from a_table 
Where [some where clause]

child.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>

答案 5 :(得分:0)

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

<ng-content select="custom-component,a"></ng-content>

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