我想用工廠來管理我的一些依賴注入,所以我在函數內部創建了一個本地類。我使用的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
類的方法的接口。
謝謝你的迴應。但是,我不能重現這一點。我無法從Record類中訪問注入的服務(例如'$ window')。 – Chic