簡體   English   中英

無法將服務注入控制器

[英]Can not Inject Service into Controller

我寫了一個服務(空的,僅用於測試目的),我想將其注入控制器。

但是,很遺憾,這無法正常工作,我看不到問題所在。

這是我的代碼:

app.controller('AppCtrl', function($scope, $location, Test){

  $scope.goto = function(destination){
    $location.path(destination);
  }

})

app.service('Test', ['', function(){

}])

這是例外:

Error: [$injector:unpr] Unknown provider: Provider <-  <- Test
http://errors.angularjs.org/1.3.13/$injector/unpr?p0=Provider%20%3C-%20%20%3C-%20Test
    at REGEX_STRING_REGEXP (ionic.bundle.js:8890)
    at ionic.bundle.js:12824
    at Object.getService [as get] (ionic.bundle.js:12971)
    at ionic.bundle.js:12829
    at getService (ionic.bundle.js:12971)
    at invoke (ionic.bundle.js:13003)
    at Object.instantiate (ionic.bundle.js:13020)
    at Object.<anonymous> (ionic.bundle.js:12881)
    at Object.invoke (ionic.bundle.js:13012)
    at Object.enforcedReturnValue [as $get] (ionic.bundle.js:12865)

我不明白問題所在

您正在注入''的依賴項,因此Angular告訴您找不到該提供程序。

相反,只需:

app.service('Test', function(){

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM