15

This example stays forever on the screen:

snack-bar-demo.ts

import {Component, ViewContainerRef} from '@angular/core';
import {MdSnackBar, MdSnackBarConfig} from '@angular/material';

@Component({
  moduleId: module.id,
  selector: 'snack-bar-demo',
  templateUrl: 'snack-bar-demo.html',
})
export class SnackBarDemo {
  message: string = 'Snack Bar opened.';
  actionButtonLabel: string = 'Retry';
  action: boolean = false;

  constructor(
      public snackBar: MdSnackBar,
      public viewContainerRef: ViewContainerRef) { }

  open() {
    let config = new MdSnackBarConfig(this.viewContainerRef);
    this.snackBar.open(this.message, this.action && this.actionButtonLabel, config);
  }
}

How can I make it disappear after 2 seconds (set duration/timeout somehow)?

KarolDepka
  • 8,318
  • 10
  • 45
  • 58
j809809jkdljfja
  • 767
  • 2
  • 9
  • 25

5 Answers5

13

With angular material 2.0.0-alpha.11, You can now add timeout to snackbar.

open() {
    let config = new MdSnackBarConfig();
    config.duration = 10;
    this.snackBar.open("Message", "Action Label", config);
}
Narendra CM
  • 1,416
  • 3
  • 13
  • 23
9

this should work

open(msg,t=2000) {
        let config = new MdSnackBarConfig(this.viewContainerRef);
        let simpleSnackBarRef = this.snackBar.open(msg, 'ok, gotcha', config);
        setTimeout(simpleSnackBarRef.dismiss.bind(simpleSnackBarRef), t);
    }
undefinederror
  • 821
  • 1
  • 8
  • 16
  • This works. Thank you!! I just wonder whether you actually do this this way to achieve designed behavior or there is something already in MdSnackBar impelemented as described here: [snack-bar](https://material.google.com/components/snackbars-toasts.html#snackbars-toasts-usage) > "They exit by being swiped off-screen or automatically disappear after a timeout or user interaction elsewhere (such as summoning a new surface or activity)." – j809809jkdljfja Oct 29 '16 at 13:53
  • I don't think there is anything like that at the moment. If you look at the [open](https://github.com/angular/material2/blob/master/src/lib/snack-bar/snack-bar.ts) method you won't find any timed dismissal procedure. That said, snack-bar status is still _Proof-of-concept_ [npm](https://www.npmjs.com/package/@angular/material). They will probably/hopefully add that good stuff in to more closely align it to the intended design specs you linked. – undefinederror Nov 04 '16 at 17:01
6
this._snackBar.open('Your Text','',
    { 
      duration: 2000
  });
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
BALU NANDAM
  • 71
  • 1
  • 1
4

The duration can be pass via the optional configuration object:

this.snackBar.open(
    this.message,
    this.action && this.actionButtonLabel, { 
        duration: 2000
    }
);
snorkpete
  • 14,278
  • 3
  • 40
  • 57
Kenton Lin
  • 41
  • 1
  • 3
1

You can do like this (angular 8+)

openMessageSnackBar("Server error", "OK");
openMessageSnackBar(message: string, action: string) {
  this._snackBar.open(message, action, {
    horizontalPosition: "center",
    verticalPosition: "left",
    duration: 5000,
  });
}
abhijeet_shela
  • 327
  • 2
  • 7