我需要将一些数据转移到子元素并在其中运行函数。 我使用ViewChild
来访问该功能。但在孩子childParam
仍未定义。Angular2。我可以同时使用ViewChild和@Input吗?
父模板:
<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 [链接](https://plnkr.co/edit/ZcZaJ0Wbu9CAobxkd2LJ?p=preview)。当我第一次点击按钮 - 没有显示任何内容时,第二次点击 - 显示字符串。 – MikeS