【问题标题】:*ngFor without wrapping inside ng-content*ngFor 不包含在 ng-content 中
【发布时间】:2018-03-28 01:04:23
【问题描述】:

我需要在另一个组件中重复一个组件,比如说:

组件 A 在其 ng-content 中只接受 B 类型的组件。

做类似的事情:

<component-a>
    <ng-template ngFor [ngForOf]="items">
        <component-B></component-B>
    </ng-template>
</component-a>

或者这个:

<component-a>
    <ng-container *ngFor="let item of items">
        <component-B></component-B>
    </ng-container>
</component-a>

没有用...

我无法修改组件-a 或组件-B,并且这些方法不会渲染组件-B...

任何想法如何克服这个问题?

谢谢。

PS:我可能需要为每个项目添加多个组件-B。

【问题讨论】:

  • 你的意思是ng-container 不是ng-template 吗?我写下了不同之处,因为我一直弄错:blog.jonrshar.pe/2017/May/20/angular-ng-elements.html
  • 将数组作为参数传递给内部组件,在内部组件的模板中重复。
  • 问题已编辑。我无法修改组件-b。
  • 您需要将ng-template 替换为ng-container
  • @Blauhirn 正如我在问题中所述,我也已经尝试过使用 ng-container。不过还是谢谢。

标签: angular angular2-ngcontent


【解决方案1】:

我通常希望像下面这样简单的东西可以工作:

<component-a>
    <component-B *ngFor="let item of items">
    </component-B>  
</component-a>

或者使用 ngForOf 语法,并不重要。您是否可以提供更多信息以便更好地查明问题所在?

【讨论】:

  • 可行,但我不能这样做,因为我可能需要为同一个项目添加多个
【解决方案2】:

解决了!通过使用

ngProjectAs="selector" 

属性在 ng-container 上!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 2018-02-27
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    相关资源
    最近更新 更多