更新类中的属性不会更新视图中的属性

时间:2017-05-22 12:13:20

标签: angular typescript

这是我在.ts Typescript文件中的代码:

 private today: Date = new Date();

这是我的HTML

  <span [innerText]="today | date:dateFormat"></span>

这显示5月22日完美

现在我有一个按钮可以从中减去一天:

previousDay() {
        Utils.addDays(this.today, -1);
        console.log(this.today);
    }

this.today在控制台中正确记录了5月21日,但我的观点仍然是5月22日。

以下是我创建的插件:https://plnkr.co/edit/6hw1JW0h5zNvF0owcU9U?p=preview

我错过了什么?

1 个答案:

答案 0 :(得分:3)

按照以下内容更新您的previousDay函数:

previousDay() {
 this.today = new Date(this.today.setDate(this.today.getDate() - 1))
 console.log(this.today); 
}

希望它有所帮助!

相关问题