Angular 访问子元素
我认为唯一的办法是提供服务,但我想知道是否还有其他办法 家长:Angular 访问子元素,angular,angular5,viewchild,Angular,Angular5,Viewchild,我认为唯一的办法是提供服务,但我想知道是否还有其他办法 家长: <h1> Parent title </h1> <child-element></childElement> 但无论如何都可以从父级访问它们?不太清楚,但如果我得到它: <h1> Parent title </h1> <child-element #child></childElement> 这允许父对象作为Javascript对象访
<h1> Parent title </h1>
<child-element></childElement>
但无论如何都可以从父级访问它们?不太清楚,但如果我得到它:
<h1> Parent title </h1>
<child-element #child></childElement>
这允许父对象作为Javascript对象访问子对象。这就是你想要的吗?太好了,正是我需要的
export class ChildComponent implements OnInit {
@ViewChildren('input') inputs;
//rest of code
}
<h1> Parent title </h1>
<child-element #child></childElement>
@ViewChild(ChildComponent) child: ChildComponent;
doSomething() {
console.log(this.child.inputs);
}