[英]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.