[英]How to detect that a bound property's value has changed in angular 4
This is how you detect that a property decorated with @Input has changed: 这是检测以@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...
}
}
}
}
Here's the markup 这是标记
<app-date-picker [(model)]="startDate"></app-date-picker>
startDate is not decorated by @Input
, but I'd like to do something if the value of startDate changes. startDate不是由
@Input
装饰的,但是如果startDate的值更改,我想做些事情。
Thanks for helping 感谢您的帮助
You can use the (onChanged) event on the date picker as follows, 您可以按以下方式在日期选择器上使用(onChanged)事件,
<app-date-picker [(model)]="startDate" (onChanged)="onDateChanged()"></app-date-picker>
and in TS
, 在
TS
,
onDateChanged() {
access the changes here
}
You can do this : 你可以这样做 :
<app-date-picker [(model)]="startDate" (modelChange)='dateChanged($event)'></app-date-picker>
reason for using (modelChange)
is simple , [(model)]
uses two way data binding for the variable bound . 使用
(modelChange)
原因很简单, [(model)]
使用两种方式对变量bound进行数据绑定。 So i can use [model]
and (modelChange)
. 所以我可以使用
[model]
和(modelChange)
。
The Other way is mentioned by @Sajeetharan . @Sajeetharan提到了另一种方式。 So Choose whichever you want .
因此,选择您想要的任何一个。 Let me know if this works.
让我知道这个是否奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.