2014-07-27 25 views
0

考慮這個例子:當我向AngularJS添加一個Typescript服務時,它會成爲一個單身人士嗎?

class AppController { 
    static $inject = [ 
     'configService' 
    ] 
    constructor(
     public config: IConfigService   // Injected into parent 
     ) { 
    } 
} 
class AdminHomeController { 
    public app: AppController; 
    static $inject = [ 
     'configService' 
    ]; 
    constructor(
     public config: IConfigService   // Injected into child 
     ) { 
    } 
} 

我注入了家長和孩子。有人可以告訴我,如果這些ConfigServices將是相同或不同的IConfigService實例Typescript類

回答

1

下面介紹一下AngularJS documentation說,關於服務:

角度服務是連接在一起使用依賴注入(DI)替代對象。您可以使用服務在整個應用中組織和共享代碼。

角度的服務宗旨是:

  • 懶洋洋地實例化 - 角只有當一個應用程序組件依賴於它實例化的服務。
  • 單例 - 每個依賴於服務的組件獲取對服務工廠生成的單實例的引用。

(重點煤礦)

+0

但是,這仍然是相同的,如果我用的打字稿類? – Alan2

+1

@Alan是一樣的 – basarat