Как установить длительность снэк-бара в angular2 (material2)
Этот пример навсегда останется на экране:
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);
}
}
Как я могу заставить его исчезать через 2 секунды (как-то задавать продолжительность/тайм-аут)?
Ответы
Ответ 1
это должно работать
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);
}
Ответ 2
С angular материалом 2.0.0-alpha.11, теперь вы можете добавить тайм-аут в закусочную.
open() {
let config = new MdSnackBarConfig();
config.duration = 10;
this.snackBar.open("Message", "Action Label", config);
}
Ответ 3
Продолжительность может проходить через необязательный объект конфигурации:
this.snackBar.open(
this.message,
this.action && this.actionButtonLabel, {
duration: 2000
}
);