Angular 2 - Using 'this' inside setTimeout [duplicate] Angular 2 - Using 'this' inside setTimeout [duplicate] javascript javascript

Angular 2 - Using 'this' inside setTimeout [duplicate]


You need to use Arrow function ()=> ES6 feature to preserve this context within setTimeout.

// var that = this;                        // no need of this linethis.messageSuccess = true;setTimeout(()=>{                           // <<<---using ()=> syntax    this.messageSuccess = false;}, 3000);