【发布时间】:2017-02-16 12:40:48
【问题描述】:
我需要将一些数据传输到子元素并在其中运行函数。
我使用ViewChild 来访问该功能。但是在子 childParam 中仍然未定义。
父模板:
<my-modal #myModal [childParam]="parentParam"></my-modal>
父组件:
@ViewChild('myModal') myModal;
parentParam: any;
onSubmit(value: any){
this.parentParam = value;
this.myModal.modalShow();
}
子组件:
@Input() childParam: any;
modalShow(){
console.log(this.childParam);
}
为什么childParam 未定义?
更好的是:直接将childParam改成ViewChild:
this.myModal.childParam = 'test string';
或者通过函数参数发送数据:
this.myModal.modalShow('test string');
【问题讨论】:
-
我猜
parentParam在调用modalShow()时尚未设置,但很难说,因为代码不允许推导出可能发生的事情。 -
我创建了 plunker link。当我第一次单击按钮时 - 没有显示任何内容,第二次单击时 - 显示了字符串。
标签: angular