javascript – Angular 2 – Using this inside setTimeout

javascript – Angular 2 – Using this inside setTimeout

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

// var that = this;                        // no need of this line
this.messageSuccess = true;

setTimeout(()=>{                           // <<<---using ()=> syntax
    this.messageSuccess = false;
}, 3000);

javascript – Angular 2 – Using this inside setTimeout

Leave a Reply

Your email address will not be published.