【问题标题】:Angular2 ViewChildren combined with transclusionAngular2 ViewChildren 结合嵌入
【发布时间】:2017-01-04 13:00:31
【问题描述】:

在 angular2 组件中使用嵌入时,我的“ViewChildren”似乎无法检测到这些组件。当我制作一个直接定义相同组件的组件时,它可以正常工作。现在有没有人可以完成这项工作?

【问题讨论】:

  • 仅供参考,我们现在使用术语内容投影而不是嵌入。
  • 感谢您的精彩补充!

标签: angular transclusion


【解决方案1】:

我也尝试使用我的错误消息组件来执行此操作。实际上你只需要使用ContentChildren 装饰器而不是ViewChildren。它专门用于 ng-content 元素。结果,您将获得QueryList,它是您的组件或元素的可迭代对象。

【讨论】:

    【解决方案2】:

    您可以使用@ContentChild()@ContentChildren() 查询嵌入的内容。

    @ViewChild() 和 ´@ViewChildren()` 只允许查询组件模板中的元素。

    另见angular 2 / typescript : get hold of an element in the template

    【讨论】:

      猜你喜欢
      • 2016-11-03
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2016-10-27
      • 2011-10-28
      • 1970-01-01
      • 2020-06-08
      • 2018-01-28
      相关资源
      最近更新 更多