Skip to content
Advertisement

Angular template interpolation

Angular version 11

I have a template which uses a method of the typescript file which returns the current date time.

The thing is that if in the interpolation of the template I call this method, the first time I render the page, it works properly, but after 1 minute, the value should change to the next minute… which it does not….

How can I make that angular refreshes the template if the result of this method is different ?

template:

JavaScript

Method in the typescript file:

JavaScript

Advertisement

Answer

You could do this:

  1. In your template, declare a variable time:
JavaScript
  1. Interpolate time
JavaScript
  1. Create an ngOnInit function and fill it with a setInterval for 60000ms (1 minute, because your time seems to display only minute). when it fires, update the time variable.
JavaScript
Advertisement