2016-07-22 77 views
2

我正在使用Typescript來處理Angular 1的Web應用程序。我必須創建一個工廠,在這個工廠中我有一些我想要注入一些依賴性的條件。這是代碼:注入不能在Typescript中工作

.factory('myFactory', function ($q: angular.IQService, $location: ng.ILocationService, $localStorage: any, Ob: any, $injector: ng.auto.IInjectorService) { 
     return { 
     f: function (config: any) { 
      config.headers = config.headers || {}; 
      if (Ob.auth) { 
       let $state = $injector.get("$state"); 
       $state.go("app.auth"); 
      } 
      return config; 
     }, 
    .... other methods.... 
     }; 
}) 

我編譯時有此錯誤控制檯:

ts\inj.ts(110,14): error TS2339: Property 'go' does not exist on type '{}'. 
ts\inj.ts(126,13): error TS2339: Property 'go' does not exist on type '{}'. 
[10:46:55] TypeScript: 2 semantic errors 
[10:46:55] TypeScript: emit succeeded (with errors) 

我該如何解決呢?

回答

0

看着this page它看起來不像$ state對象有一個go()方法,這就是TypeScript引發錯誤的原因。你的意思是使用$state.get()代替嗎?

+0

我想將用戶重定向到該狀態,並且'get'它給了我同樣的錯誤 – panagulis72

+0

即使用'get'替換它們,完全相同的錯誤也完全一樣嗎?即使你的代碼中沒有'go'? – Aron