我有 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>
谢谢。
最佳答案
在括号内,您应该放置用 @Output 装饰器装饰的属性的名称。在本例中(元素)
将您的parent.html更改为:
<app-child (element)="receiveContent($event)"></app-child>
关于javascript - Angular 7 : passing parameters to other component error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54828434/