使用setDate
方法修改的日期对象未在模板中更新.
在模板中:@H_502_3@
<p>{{date | date:'mediumDate'}}</p>
在组件中:@H_502_3@
nextDay(){
this.date.setDate(this.date.getDate()+1);
}
但是当我调用nextDay函数时,模板不会使用新值更新.@H_502_3@
我可以让变更检测工作的唯一方法是这样做:@H_502_3@
nextDay(){
var tomorrow = new Date();
tomorrow.setDate(this.date.getDate()+1);
this.date = tomorrow;
}
有没有更好的方法来完成同样的任务?@H_502_3@
我认为这是改变日期变量引用的正确
方法.从文档
here我们得到:
The default change detection algorithm looks for differences by comparing bound-property values by reference across change detection runs.@H_502_3@
因此,如果日期参考保持不变,则不会发生任何事情.你需要一个新的Date引用,这就是nextDay()的第二个版本工作的原因.@H_502_3@
如果删除格式化管道,您将看到仍然只有第二个版本的nextDay()工作.@H_502_3@