【发布时间】:2018-10-31 12:31:33
【问题描述】:
我正在使用 Angular 版本 6 构建一个简单的基于 Web 的应用程序。
在我的应用程序中有一个包含子组件的组件。此组件中有一个函数(在父组件中,而不是子组件中。)我想使用子组件中的按钮调用该函数。
这张图片解释了我的组件的格式。
我认为它与角度 @Output 有关。但我无法管理它。
这就是我的代码的组织方式。
父组件 - component.ts 文件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-teacher-home',
templateUrl: './teacher-home.component.html',
styleUrls: ['./teacher-home.component.scss']
})
export class TeacherHomeComponent implements OnInit {
constructor() { }
ngOnInit() {
}
formView: boolean = false;
toggleForm(){
this.formView = !this.formView;
}
}
父组件 - component.html 文件
<div>
<child-compnent></child-compnent>
</div>
子组件 - component.html 文件
<div>
<button>Toggle Form view</button>
</div>
当点击子组件中的按钮时,我想调用父组件的函数toggleForm()。
【问题讨论】:
-
检查 Angular 文档,特别是 Component Interaction 部分。另外,在询问之前做一些研究,一次搜索就会返回一堆涵盖您的用例的答案......
标签: javascript angular typescript components