从child angular2中的父级访问变量

时间:2017-01-28 13:46:32

标签: angular

我的父组件中有一个变量,我想在我的子组件中编辑。 我尝试ienuInt,但它似乎不起作用!如何实现传递var给孩子并听取改变?

父:

ienuInt

子:

@Input

谢谢。

1 个答案:

答案 0 :(得分:5)

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"

应该做你想做的事。