我有一個簡單的應用程序,我正在Angular開發。我儘可能簡化了以下內容。我試圖做一個globally available library injectible(沒有什麼特別):模塊間通信,Angular Js,工廠
我app.module.js
文件被簡單地定義爲:
angular.module('dogApplication', [
'dogModule',
'dogSelection'
]);
的index.html
文件的定義如下(去內臟爲了便於閱讀):
<html lang="en" ng-app="dogApplication">
<head>
<!-- ... -->
<script src="app/app.module.js"></script>
<script src="app/dogs/dog.module.js"</script>
<script src="app/dogs/dog.factory.js"</script>
<script src="app/dogSelection.module.js"></script>
<!-- controllers then imported -->
</head>
<body>
<!-- content -->
</body>
</html>
.module
文件定義了每個附加組件,控制器,工廠等附加的模塊,其中app.module.js
是「主要」模塊附件。
dog.factory.js
定義如下:
angular
.module('dogModule')
.factory('dog', function($window) {
// do logic defined by James Hill in linked article
return friendly;
});
然後我有一個控制器:
angular
.module('dogSelection')
.controller('dogSelectionController', ['$scope', 'dog', function ($scope, dog) {
if (dog)
{
console.log('it worked');
}
}])
當訪問應用程序時,我得到一個糟糕的錯誤信息,我不能很決心:
angular.js:13920 Error: [$injector:unpr] Unknown provider: dogProvider <- dog <- dogSelectionController
http://errors.angularjs.org/1.5.8/$injector/unpr?p0=dogProvider%20%3C-%20dog%20%3C-%20dogSelectionController
at angular.js:68
at angular.js:4511
at Object.getService [as get] (angular.js:4664)
at angular.js:4516
at getService (angular.js:4664)
at injectionArgs (angular.js:4688)
at Object.invoke (angular.js:4710)
at $controllerInit (angular.js:10354)
at nodeLinkFn (angular.js:9263)
at angular.js:9673
隨着我有上述設置的方式,爲什麼dog
服務而不是通過DI可用於dogSelectionController
?
我知道.factory(//...
方法中的代碼可以工作,因爲我可以將它附加到控制器定義的末尾;但是,我希望工廠在整個應用程序中都可用。這可能嗎?
您已將它定義爲注射陣列上的'leaflet',但是在實際參數中是'dog'。我相信他們需要匹配的工作。 – Lex
@Lex他們不需要匹配。唯一的限制是字符串中的名稱必須與服務的實際名稱匹配 – gmustudent
@Lex這是一個帖子錯誤,對不起。我需要解決這個問題。試圖使其更廣泛適用。 – Thomas