2016-12-20 4 views
1

나는 여러 스낵을위한 기간을 설정해야하지만 너무각도 2 플로팅 작업은 - 글로벌 지속 시간은 구성

let config = new MdSnackBarConfig(); 
config.duration = 5000; 

this.snackBar.open(element.text, 'OK', config); 

같은 플로팅 작업 메시지의 시간을 설정할 수 있으며, 오히려 때마다 설정에 통과하지 않았을 .

어쨌든 전역 기간 설정을 설정할 수 있습니까?

감사합니다.

답변

2

우리가 한 일은 외부 모듈 app.config.ts 파일을 모듈 수준에서 포함하고 필요한 곳에 그것을 포함시킵니다. 이것은 파일에있는 내용의 예입니다.

export class Config { 
    static apiUrl = "api/"; 
    static token = ""; 
    static snackBarDurration = 5000; 
    ...... 
} 

그런 다음 모듈에 선언 한 다음 사용하려는 구성 요소 또는 서비스로 가져 오기 만하면됩니다. 다음은 그 예입니다.

import { Injectable } from "@angular/core"; 
import { Config } from "../../app.config"; 

@Injectable() 
export class SnackBarService { 

    .... // declare your snackbar here 

    constructor() { } 

    showSnackBar(elementText: string){ 
     let config = new MdSnackBarConfig(); 
     config.duration = Config.snackBarDurration; // Here is your change 

     this.snackBar.open(elementText, 'OK', config); 
    } 
} 
1

나는 그것이 글로벌 구성에 대한 해결책이 아니다 알고 있지만,보다 컴팩트 한 메이크업 호출에 나는 PARAMS에서 설정 선언 :

this.snackBar.open(elementText, 'OK', { duration: 3000 });