可以在 ngFor 中使用 ng-content 吗? [英] Can an ng-content be used inside of an ngFor?

查看:31
本文介绍了可以在 ngFor 中使用 ng-content 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想在 *ngFor 里面放一个 ng-content 然后指定 ng-content 的内容,当使用包含 ngFor 的组件.可能吗?

  • <ng-content></ng-content>
  • 演示

    解决方案

    这是可能的,但可能不会产生预期的结果.父级传递的子级只能投影一次(无论有多少.如果 元素没有选择使用 select 属性传递子项的特定和不同部分,然后使用默认选择器(无)将所有内容投影到第一个 .>

    为了完成这项工作,您可能需要一个自定义 ngFor 来重复传递给 的内容.

    NgFors ngForTemplate 可能帮助您的用例.

    另见 Angular2 子组件作为数据

    We'd like to put a ng-content inside of an *ngFor and then specify the contents of the ng-content when the component containing the ngFor is used. Is it possible?

    <li *ngFor="let data of dataSource">
      <ng-content></ng-content>
    </li>
    

    Demo

    解决方案

    It is possible but probably doesn't produce the desired result. Children passed by the parent can only projected once (no matter how many <ng-content> are there. If the <ng-content> elements don't select specific and different parts of the passed children using the select attribute, then everything is projected to the first <ng-content> with the default selector (none).

    To make this work you probably want a custom ngFor that repeats the content passed to <ng-content>.

    NgFors ngForTemplate might help in your use case.

    See also Angular2 child component as data

    这篇关于可以在 ngFor 中使用 ng-content 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆