簡體   English   中英

Angular2-檢測組件子變量的變化

[英]Angular2 - detect component child variable change

父組件

import { Component, OnChange } from '@angular/core';
import { ChildComponent } from '../../reusable/user-offers/user-offers.component';
@Component({
  ...
  directives: [ ChildComponent ]
})
export class ParentComponent implements OnChange {
  @ViewChild(ChildComponent)
    private child: ChildComponent;

  ngOnChanges() {
    detecting this.child.detectme variable...
  }

}

子組件

import { Component, Input } from '@angular/core';
@Component({
  ...
})
export class ParentComponent {    
  @Input() detectme: string;
  //in view something changes detectme value (not important here)
}

如何檢測child.detectme是否已更改並打印其值?

您想在父組件中檢測到子組件中的值已更改,對嗎? 如果是這樣,則應在子組件中使用EventEmitter並發出事件。 然后,使用(eventNameFromChild)="handler($event)"語法在父組件中訂閱事件。

為您創建了一個小品

暫無
暫無

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

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