0

I'm trying to make a custom toast notification system in Angular, but I've run into a problem. So what should happen is that I run a function on the service, which passes down a string of text, the text is pushed to the service so it can be used globally across the app and a boolean value switches to true which shows the toast and then after a timeout switches to false so it hides again. It all works right up to the point where the toast hides itself again. I did a console log on the bool value and it's switching as it should do so that's not the problem.

My toast is in it's own component and it's html is as so:

<div *ngIf="badge.showToast" id="toast">
  <div id="icon-wrapper">
    <i class="fa fa-check"></i>
  </div>
  <div id="text-wrapper">
      <p id="toast-title">{{badge.badgeTitle}}</p>
      <p id="toast-text">{{badge.badgeEarned}}</p>
  </div>
</div>

And my service is pretty straightforward:

import { Injectable } from '@angular/core';

@Injectable()
export class BadgeService {

  badgeTitle: string = "You earned a Badge!";
  badgeEarned: string;
  showToast: boolean = false;

  constructor() { }

  rewardBadge(badgeEarned: string) {
    this.badgeEarned = badgeEarned;
    this.showToast = true;
    setTimeout(function(){ this.showToast = false; console.log(this.showToast); }, 5000);
  }

}

And I'm calling the function from the app.component.ts for testing:

ngOnInit() {
    this.getUserProfile();
    this._badge.rewardBadge("Test badge text");
}

Surely if the showToast bool has changed to false (which the console log said it did) then I thought it should hide the div layer in the other component?

Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101
  • It doesn't switch to false, because it's not using the correct `this`. Use an arrow function. – JB Nizet Sep 14 '18 at 19:54
  • Sorry I'm still confused - I thought that because the console log was displaying false that meant it had changed to false? I'm not sure where I'd use the arrow function or the difference it could make if `this.showToast` was already set to false? – Web Develop Wolf Sep 14 '18 at 19:59
  • That's why I posted a link to a duplicate question which explains all of that. – JB Nizet Sep 14 '18 at 20:02
  • @WebDevelopWolf - It does not modify the `showToast` member of `BadgeService`. It creates a property `showToast` on the global object. Use the syntax: `setTimeout(() => { ... }, 5000);`. – ConnorsFan Sep 14 '18 at 20:04
  • @ConnorsFan - Ahhh thanks that makes much more sense - if you wanna stick that in as the answer (either of you) then I'll mark it as correct if you like? – Web Develop Wolf Sep 14 '18 at 20:07
  • This problem is reported very very often on the Angular forum (e.g. [that question](https://stackoverflow.com/q/52338528/1009922) was asked 10 minutes after yours). That's why we find the duplicate very quickly. You can accept the duplicate. – ConnorsFan Sep 14 '18 at 20:09
  • No worries - done :) – Web Develop Wolf Sep 14 '18 at 20:11

0 Answers0