我的父组件中有一个变量,我想在我的子组件中进行编辑。
我尝试了 @Input
但似乎不起作用!我怎样才能实现将var传递给 child 并监听变化?
父级:
export class Parent implements OnInit {
showModal : boolean = false;
}
child :
export class Child implements OnInit {
@Input() showModal: boolean;
// calling this function on (click) button.
closeModal() {
this.showModal = false;
}
}
谢谢。
最佳答案
export class Child implements OnInit {
@Input() showModal: boolean;
@Output() showModalChange:EventEmitter<boolean> = new EventEmitter<boolean>();
// calling this function on (click) button.
closeModal() {
this.showModal = false;
this.showModalChange.emit(this.showModal);
}
}
父元素html
<child [(showModal)]="showModal"
应该做你想做的事。
关于angular - 在子 Angular2 中访问父级的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41910506/