in Education by
This question already has answers here: How to access the correct `this` inside a callback (13 answers) Closed 3 years ago. I am new in javascript and angular. This should be a simple issue but I don't know what is wrong with my code. I don't know if i have the proper syntax for the time handler. The code just display the time only once. import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-hello-world', templateUrl: './hello-world.component.html', styleUrls: ['./hello-world.component.css'] }) export class HelloWorldComponent implements OnInit { dateMessage: string; constructor() { setInterval(this.time_handler, 1000); //Method 1: This works // setInterval(()=> { // let currentDate = new Date(); // this.dateMessage = currentDate.toLocaleTimeString(); // }, 1000); } ngOnInit() { } // Method 2: This does not work public time_handler() { let currentDate = new Date(); this.dateMessage = currentDate.toLocaleTimeString(); } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You need to bind this to the function inside the interval. constructor() { setInterval(this.time_handler.bind(this), 1000); //Method 1: This works // setInterval(()=> { // let currentDate = new Date(); // this.dateMessage = currentDate.toLocaleTimeString(); // }, 1000); } Else this in the handle method won't point to the class.

Related questions

0 votes
    This question already has an answer here: Object.assign getters and setters in constructor (1 answer) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: Object.assign getters and setters in constructor (1 answer) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: JavaScript: Difference between .forEach() and .map() (15 answers) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: When to use ":"(colon) operator in javascript vs "=" operator ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: When to use ":"(colon) operator in javascript vs "=" operator ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have a mobile website that plays a background song. When I pressed the home button on my phone ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have created a partial view, the element in the partial view looks like this (Razor Code): @Html ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I have created a partial view, the element in the partial view looks like this (Razor Code): @Html ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    So, I am using Shopify for a clients eCommerce store and I'm creating a node js program that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    This is my pretty simple C++/CMake .travis.yml env: global: # The next declaration is the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I installed PEAR and when I try to run it, I receive this message: PHP_PEAR_PHP_BIN is not set correctly ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I'm trying to open up a PDF as an image using Wand. If I run the code below in Jupyter Notebook ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
...