簡體   English   中英

從子angular2中的父級訪問變量

[英]Access variable from parent in child angular2

我在父組件中有一個變量,我想在子組件中進行編輯。 我嘗試了@Input但似乎沒有用! 如何實現將var傳遞給孩子並聽取更改?

家長:

export class Parent implements OnInit {
    showModal : boolean = false;
}

兒童:

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"

應該做你想做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM