Angular2 ViewChildren 结合嵌入

Angular2 ViewChildren combined with transclusion

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

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

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

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

另见