我在我的HTML應用程序 打字稿角依賴注入失敗
/// <reference path="../Scripts/angular.d.ts" />
/// <reference path="testCtrl.ts" />
/// <reference path="testSvc.ts" />
angular.module('testApp', []);
angular.module('testApp').constant('url','http://whatever');
angular.module('testApp').factory(
'testSvc', ['$http', 'url', testApp.testSvc]);
angular.module('testApp').controller(
'testCtrl', ['testSvc', testApp.testCtrl]);
和控制器
/// <reference path="../Scripts/angular.d.ts" />
/// <reference path="testSvc.ts" />
module testApp{
export class testCtrl {
public someText:string;
public httpData:any;
public urlName:string;
private data:IData;
static $inject = ['testSvc'];
constructor(testSvc) {
this.someText = 'I am in scope'
this.data = testSvc.getData();
this.httpData = {
putFunc: this.data.putFunc,
expectPUTFunc: this.data.expectPUTFunc
}
this.urlName = this.data.urlName;
}
}}
和服務
/// <reference path="../Scripts/angular.d.ts" />
/// <reference path="testCtrl.ts" />
interface IData{
urlName:string;
putFunc:string;
expectPUTFunc:string;
}
module testApp{
export class testSvc {
public data:IData;
static $inject = ['$http', 'url'];
constructor(private $http, url) {
this.data = {
urlName:url,
putFunc: typeof $http.put,
expectPUTFunc: typeof $http.expectPUT
}
}
getData(){
return this.data;
}
}}
這些腳本聲明
<script type="text/javascript" src="../Scripts/angular.js"></script>
<script src="testSvc.js"></script>
<script src="testCtrl.js"></script>
<script src="testApp.js"></script>
當我運行此應用程序控制器從未收到對testSvc.getData(其依賴),因爲testSvc是不確定的。我哪裏錯了?
你爲什麼對角頂分層你自己的模塊?你從這個額外的複雜層面,額外的全局狀態和語義重複中獲得了什麼好處? – yangmillstheory