Tim Liberty Tim Liberty - 1 year ago 43
TypeScript Question

Updating property in class does not update the property in view

This is my code in .ts Typescript file:

private today: Date = new Date();

and this is my html

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

This shows 22nd May perfectly

now I have a button to subtract a day from it:

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

correctly logs 21st May in the console however my view stays at 22nd May.

Here is the plunk that I have created: https://plnkr.co/edit/6hw1JW0h5zNvF0owcU9U?p=preview

What am I missing?

Answer Source

Update your previousDay function as below as per plunker:

previousDay() {
 this.today = new Date(this.today.setDate(this.today.getDate() - 1))

Hope it helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download