[英]Pass value from Child component to Parent component , after child is initialized
现在我正在将值从子级传递给父级,同时单击发送按钮,如何传递相同的数组子级已初始化,我尝试使用 'ngAfterViewInit' 但没有奏效,这里有任何建议。
child
@Component({
selector: 'app-child',
templateUrl: './child.component.html'
})
export class ChildComponent {
cars = ["Saab", "Volvo", "BMW", "BMW", 'Saab'];
@Output() msEvent = new EventEmitter();
constructor() {}
send() {
this.msEvent.emit(this.cars);
}
// ngAfterViewInit() {
// this.msEvent.emit(this.cars);
// }
}
}
child.component.html : <button (click) = 'send()' class="button">send</button>
----------------------------------------
parent
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
cars:any[];
constructor() {
}
rece($event) {
this.cars = $event;
}
}
app.component.html : <app-child (msEvent) = 'rece($event)' [employees] = 'employees'></app-child>
ngOnInit() 应该足以满足您的情况,因为您传递的数据不依赖于“视图”。 在这里查看: StackBlitz 示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.