在Angular中,如何使用ViewChild / ViewChildren来生孩子?

时间:2019-05-23 19:45:28

标签: angular

@ViewChildren(Subcomponent) sComp;

将让我获得当前组件的子组件子代,但我想获取作为子组件子代的元素的值。我该怎么办?

1 个答案:

答案 0 :(得分:2)

Subcomponent中,您可以定义一个方法来返回其子级:

@ViewChildren(ChildA) childAOfSubcomponent;

getChildA() {
  return this.childAOfSubcomponent;
}

现在在当前组件中使用此:

this.sComp.getChildA();  // thi will return child of SubComponent