miraxes / angular-custom-tour

Custom tour hints
24 stars 12 forks source link

Auto start not working #5

Closed aimingxu closed 6 years ago

aimingxu commented 6 years ago

I want to auto start the tour after page load without click a button or link. Here is my code: ngAfterContentInit(){ setTimeout(function(){ this.hintService.initialize(); }, 4000); } I got ERROR TypeError: Cannot read property 'initialize' of undefined.

miraxes commented 6 years ago

@aimingxu you just losing a this context . There are 2 ways to avoid it

  1. bind this to some variable (old style)

    var self = this;
    setTimeout(function(){
    self.hintService.initialize();
    }, 4000);
    }
  2. And ES6 one is using lambda(arrow)

    setTimeout(() => {
    this.hintService.initialize();
    }, 4000);
    }
miraxes commented 6 years ago

@aimingxu

Feels like solved, closing.