Tekk_Know Tekk_Know - 2 months ago 32x
AngularJS Question

Angular 2 - Component property changes but view does not update

I'm attempting to learn Angular 2. My understanding is that interpolated items in the view update when their corresponding variable is updated in the model, however in the code below I do not see this behavior.

import { Component, OnInit } from '@angular/core';

selector: 'ai-header',
templateUrl: 'app/header/header.component.html',
styleUrls: ['app/header/header.component.css'],

export class HeaderComponent implements OnInit {
currentTime: Date = new Date();

getCurrentTime(): void{
let dateTime = new Date();
let year = dateTime.getFullYear();
let month = dateTime.getMonth();
let day = dateTime.getDate();
let hour = dateTime.getHours();
let minute = dateTime.getMinutes();
let second = dateTime.getSeconds();
dateTime = new Date(year, month, day, hour, minute, second)

this.currentTime = dateTime;

ngOnInit(): void {
setInterval(this.getCurrentTime, 1000);

Why is the view not updating when the 'currentTime' property of the component is updated?


I think it should be:

ngOnInit(): void {
    setInterval(this.getCurrentTime.bind(this), 1000);