2015-10-19 73 views
0

我想用工廠來管理我的一些依賴注入,所以我在函數內部創建了一個本地類。我使用的JavaScript框架(AngularJS)會將函數的返回值注入到構造函數中。在工廠函數外部公開類型的本地TypeScript類

如何從工廠函數外引用返回類Record的類型?

/** Define factory to create Record classes */ 
export default function RecordFactory($window) { 
    return class Record { // I want to reference this class as a type 
    doSomething() { 
     // use $window service 
    } 
    } 

} 

/** 
* Controller for page with returned class of RecordFactory injected 
* into the constructor 
*/ 
class RecordPageCtrl { 
    record 
    constructor(Record) { // How do I specify the type of Record here? 
    this.record = new Record(); 
    } 
} 

// Dependency Injection is handled by AngularJS 
angular.module('myApp', []) 
    .factory('Record', RecordFactory) 
    .controller('RecordPageCtrl', RecordPageCtrl) 

注:我試圖避免與維護上的所有Record類的方法的接口。

回答

0

這適用於我。

namespace Factored { 
    // Class Record wrapped in namespace only to distinguish from other Record usages 
    export class Record { // I want to reference this class as a type 
    doSomething() { 
     // use $window service 
    } 
    }; 
} 

/** Define factory to create Record classes */ 
export default function RecordFactory($window) { 
    return Factored.Record; // return class reference 
} 

/** 
* Controller for page with returned class of RecordFactory injected 
* into the constructor 
*/ 
class RecordPageCtrl { 
    record: Factored.Record; 
    constructor(Record: typeof Factored.Record) { // Referencing namespaced class 
    this.record = new Record(); 
    this.record.doSomething(); 
    } 
} 

// Dependency Injection is handled by AngularJS 
angular.module('myApp', []) 
    .factory('Record', RecordFactory) 
    .controller('RecordPageCtrl', RecordPageCtrl) 
+0

謝謝你的迴應。但是,我不能重現這一點。我無法從Record類中訪問注入的服務(例如'$ window')。 – Chic

0

我重新考慮了工廠是如何創建的,服務被注入類如Record。通過構造函數傳遞服務,工廠可以輕鬆地傳遞服務並允許AngularJS處理依賴注入。

/** Class where service needs to be injected */ 
class Record { 
    constructor(private myInjectedService) {} 
    doSomething() { 
     // use myService 
     this.myInjectedService.log('Hello World'); 
    } 
} 

/** Define factory to create Record class instances */ 
function RecordFactory(MyService) { 
    return new Record(MyService); // return a new class 
} 

/** 
* Controller for page 
* RecordFactory return variable is injected into the constructor 
*/ 
class RecordPageCtrl { 
    constructor(public Record: Record) { 
    this.Record.doSomething(); 
    } 
} 

/** Service to inject into Record class */ 
class MyService { 
    log(message: string) { 
     console.log(message); 
    } 
} 

let myServiceInst = new MyService(); 

// directly instantiated 
let factoryInstance = RecordFactory(myServiceInst); 
new RecordPageCtrl(factoryInstance); 

// Dependency Injection handled by AngularJS 
angular.module('myApp', []) 
    .factory('Record', RecordFactory) 
    .service('MyService', MyService) 
    .controller('RecordPageCtrl', RecordPageCtrl) 

注意,如果你希望能夠創建Record類在工廠被注入的多個實例,您將需要有工廠返回另一家工廠叫時實例化的功能。