[英]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.