[英]Get Value from View Child Component (from Child to Parent)
您可以使用@ViewChild指令访问父组件中的子组件数据。
ParentClass implements OnInit{
@ViewChild(QuestionComponent) questionComponent: QuestionComponent;
ngOnInit() {
this.getQuestions(); // you can call this function wherever you need it
// just, for example, I am calling it from ngOnInit
}
getQuestions() {
if (questionComponent) {
console.log(questionComponent.questions); // like this way we can access child component variables and functions in parent class
}
}
}
希望这会帮助你。
注意:仅当您只有一个QuestionComponent作为孩子时,此方法才有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.