10

यह उदाहरण स्क्रीन पर हमेशा के लिए रहता है सेट कर सकते हैं:मैं कैसे 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 सेकंड के बाद गायब हो जाते हैं कर सकते हैं (सेट किसी भी तरह की अवधि/टाइमआउट)?

उत्तर

7

इस काम करना चाहिए

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); 
    } 
+0

यह काम करता है। धन्यवाद!! मुझे आश्चर्य है कि क्या आप वास्तव में डिज़ाइन किए गए व्यवहार को प्राप्त करने के लिए ऐसा करते हैं या यहां कुछ ऐसा है जो एमडीएसएकबैर में पहले से वर्णित है जैसा कि यहां वर्णित है: [स्नैक-बार] (https://material.google.com/components/snackbars-toasts। एचटीएमएल # स्नैकबार-टोस्ट्स-यूज) > "वे ऑफ़-स्क्रीन को स्वाइप करके बाहर निकलते हैं या किसी टाइमआउट या उपयोगकर्ता इंटरैक्शन के बाद स्वचालित रूप से गायब हो जाते हैं (जैसे नई सतह या गतिविधि को बुलावा)।" – johnerfx

+0

मुझे नहीं लगता कि फिलहाल ऐसा कुछ भी है। यदि आप [open] (https://github.com/angular/material2/blob/master/src/lib/snack-bar/snack-bar.ts) विधि देखते हैं तो आपको कोई समय बर्खास्तगी प्रक्रिया नहीं मिलेगी। उस ने कहा, स्नैक-बार स्थिति अभी भी _Proof-of-concept_ [npm] है (https://www.npmjs.com/package/@angular/material)। वे शायद/उम्मीदपूर्वक उस अच्छी चीज को आपके द्वारा लिंक किए गए इच्छित डिजाइन चश्मे पर अधिक बारीकी से संरेखित करने के लिए जोड़ देंगे। – undefinederror

10

कोणीय सामग्री 2.0.0-alpha.11 के साथ, अब आप Snackbar को टाइमआउट जोड़ सकते हैं।

open() { 
    let config = new MdSnackBarConfig(); 
    config.duration = 10; 
    this.snackBar.open("Message", "Action Label", config); 
} 
+0

वाह, आप जल्दी थे। मैं इसे भी अपडेट करना चाहता था, बस रिलीज की प्रतीक्षा कर रहा था :) धन्यवाद। – johnerfx

1

अवधि वैकल्पिक विन्यास वस्तु के माध्यम से पारित किया जा सकता है:

this.snackBar.open(
    this.message, 
    this.action && this.actionButtonLabel, { 
     duration: 2000 
    } 
); 
+0

संपादन snorkpete के लिए धन्यवाद –

संबंधित मुद्दे