草庐IT

javascript - 如何检测 Angular2 中 Date 对象的变化?

使用setDate方法修改的日期对象不会在模板中更新。在模板中:{{date|date:'mediumDate'}}在组件中:nextDay(){this.date.setDate(this.date.getDate()+1);}但是当我调用nextDay函数时,模板不会更新为新值。我能让变化检测工作的唯一方法是这样做:nextDay(){vartomorrow=newDate();tomorrow.setDate(this.date.getDate()+1);this.date=tomorrow;}是否有更好的方法来完成同样的任务? 最佳答案

慕测-软件测试NextDay

QUESTIONpackagenet.mooctest;publicclassDate{ privateDayd; privateMonthm; privateYeary; publicDate(intpMonth,intpDay,intpYear){ y=newYear(pYear); m=newMonth(pMonth,y); d=newDay(pDay,m); } publicvoidincrement(){ if(!d.increment()){ if(!m.increment()){ y.increment(); m.setMonth(1,y); }