【发布时间】:2023-03-13 07:26:01
【问题描述】:
我有 2 个组件,一个父组件和一个子组件,我想将变量的值发送到父组件,我尝试了下面的代码但没有成功。子组件正确发送信息,但是父组件没有接收,看来父组件中的函数没有识别接收到要触发的数据。
子组件
...
private elements:any;
@Output() element = new EventEmitter<any>();
constructor() {
}
ngOnInit() {
this.sendContent();
}
sendContent() {
this.elements = "hi";
console.log("sended");
console.log(this.elements);
this.element.emit(this.elements);
//the function is activated and I can see the return in the console
}
父组件
...
constructor() {
}
ngOnInit() {
}
receiveContent(elements) {
console.log("received");
console.log(elements);
//the function is not activated when the child component is sent the data
}
父模板
<app-child (sendContent)="receiveContent($event)"></app-child>
谢谢。
【问题讨论】:
标签: javascript angular typescript events components