[英]How to detect that a bound property's value has changed in angular 4
这是检测以@Input装饰的属性已更改的方式:
export class EmployeeComponent implements OnChanges {
@Input() message: string;
startDate: Date;
ngOnChanges(changes: SimpleChanges) {
for (let propName in changes) {
let change = changes[propName];
if (propName === 'message') {
//do something...
}
}
}
}
这是标记
<app-date-picker [(model)]="startDate"></app-date-picker>
startDate不是由@Input
装饰的,但是如果startDate的值更改,我想做些事情。
感谢您的帮助
您可以按以下方式在日期选择器上使用(onChanged)事件,
<app-date-picker [(model)]="startDate" (onChanged)="onDateChanged()"></app-date-picker>
在TS
,
onDateChanged() {
access the changes here
}
你可以这样做 :
<app-date-picker [(model)]="startDate" (modelChange)='dateChanged($event)'></app-date-picker>
使用(modelChange)
原因很简单, [(model)]
使用两种方式对变量bound进行数据绑定。 所以我可以使用[model]
和(modelChange)
。
@Sajeetharan提到了另一种方式。 因此,选择您想要的任何一个。 让我知道这个是否奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.