2016-11-25 24 views
0

我有一個函數來檢測當前區域設置,我需要通過提供程序設置角度爲2的rooot App模塊的區域設置。這樣我可以在所有其他組件中使用它。檢測區域設置並將其設置在使用提供程序的角度2根模塊

我知道我可以像下面這樣做。

{提供: '區域設置',useValue: 'EN-US'}

但我想這樣做下面如果可能的話。

{提供: '區域設置',useValue:this.detectLocale()}

否則請提出合適的方式來實現這一目標。謝謝。

回答

1

有一個AppConfig提供程序將存儲配置和其他常量值可能會在您的應用程序中很有用。

import { Injectable } from "@angular/core" 

@Injectable() 
export class AppConfig() 
{ 
    public endPointUrl: string; // sample configuration value 
    public local: string; 

    constructor(){ 
     this.endpointUrl = ""; 
     this.local = ""; // use detect local logic here 
    } 
} 

,並提供您的配置類的單一instanse:

{provide: AppConfig, useValue: new AppConfig()} 

通過這種方式,你可以把你所有的設置和配置,在同一個地方。

也就像我現在你不能使用string s作爲提供者標記。提供者令牌只能是類類型。