问题描述
好吧,在解决了这个臭名昭著的问题之后,我现在正式秃顶了:缩小的 AngularJS 应用程序无法正常工作,解决了这个错误:
OK I'm officially bald now, after having been streching my hair out with this infamous problem: The minfied AngularJS app just doesn't work, with this error thown out:
错误:[$injector:unpr] 未知提供者:aProvider <- ahttp://errors.angularjs.org/1.2.6/$injector/unpr?p0=a提供者%20%3C-%20a在 http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:11492在 http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26946在 Object.c [as get] (http://localhost/我的应用程序/dist/scripts/1bde0e2e.vendor.js:4:26250)在 http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:27041在 c (http://localhost/my-app/dist/脚本/1bde0e2e.vendor.js:4:26250)在 Object.d [作为调用] (http://localhost/我的应用程序/dist/scripts/1bde0e2e.vendor.js:4:26496)在 http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:9:910在 Object.f [as forEach] (http://localhost/我的应用程序/dist/scripts/1bde0e2e.vendor.js:4:11927)在 http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:9:856在 j (http://localhost/my-app/dist/脚本/1bde0e2e.vendor.js:5:27235)
Error: [$injector:unpr] Unknown provider: aProvider <- a http://errors.angularjs.org/1.2.6/$injector/unpr?p0=aProvider%20%3C-%20a at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:11492 at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26946 at Object.c [as get] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26250) at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:27041 at c (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26250) at Object.d [as invoke] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26496) at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:9:910 at Object.f [as forEach] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:11927) at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:9:856 at j (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:5:27235)
很多其他人也有这个问题,但看起来可以通过将依赖项声明为数组而不是裸函数参数来解决这个问题,如下所示:
Lots of other people had this problem as well, but looks like it could be fixed by declaring dependencies as an array instead of bare function parameters, like this:
angular.module('my-app').controller('LoginCtrl', [ '$scope', 'HttpService', function($scope, HttpService) { ... }]);
而不是这个:
angular.module('my-app').controller('LoginCtrl', function($scope, HttpService) { ... });
但在我的情况下它不起作用.我检查了我所有的脚本(咖啡和生成的 javascript),它们都使用了正确的数组样式声明.
But it doesn't work in my case. I checked all of my scripts (coffee and generated javascripts as well), they all use the proper array-style declaration.
问题显然不是来自额外的包.我尝试将所有额外的包引用移出 <!-- bower:js -->
块(这样它们就不会被 grunt 缩小),但问题仍然存在.这意味着,这是我的代码的错……但话又说回来,我尝试了(似乎是)唯一可用的修复程序,但无济于事.
The problem doesn't come from extra packages apparently. I tried moving all extra package references out of <!-- bower:js -->
block (so that they are not minified by grunt), yet the problem still remains. Which means, it's my code to blame... But then again, I've tried the (seems to be) only fix available, to no avail.
任何提示,甚至关于如何正确调试它?
Any hint, even on how to properly debug this?
提前致谢!
推荐答案
终于找到问题所在了.是的,这是一个我错过的 DI 错误.
Finally I've found the problem. And yes, it's a DI bug which I missed.
对于所有可能遭受同样头痛的人:数组格式声明也必须在 $routeProvider
的 resolve
选项中完成.在我的情况下(CoffeeScript 前面):
To all who may be suffering from the same headache: array-format declaration must be done in $routeProvider
's resolve
options as well. In my case (CoffeeScript ahead):
app.config (['$routeProvider', ($routeProvider) ->
$routeProvider
.when '/',
templateUrl: 'views/main.html'
controller: 'MainCtrl'
resolve:
groups: ['GroupService', (GroupService) -> # I MISSED THIS
return GroupService.getAll()
]
entries: ['EntryService', (EntryService) -> # AND THIS
return EntryService.getAll()
]
# ...
])
希望这会有所帮助!
这篇关于AngularJS 错误:未知提供者:aProvider <- a的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!