我一直在对我的heroku构建进行故障排除,并且无法确定这个 Angular 模块没有实例化的原因。当我在本地主机上运行我的应用程序时,一切都运行良好,所以我假设错误发生在 Grunt 的丑化过程中。我所有类似的 Controller 模块似乎都加载良好,我可以在开发工具的“源”选项卡中看到它们。但是这个 Controller 文件没有显示出来,我猜是因为它里面有一些错误。
这是未实例化的模块( Controller ):
angular.module('inspector.location', [])
.controller('LocationDisplayController', function($scope, $http, $routeParams,
ResultService) {
var controller = this;
this.inspections = ResultService.inspections;
});
它看起来像丑陋的(我将 mangle 设置为 false 以尝试解决此问题): Angular 模块(“检查器.位置”, []).controller("LocationDisplayController",function($scope,$http,$routeParams,ResultService){this.inspections=ResultService.inspections})
这是它唯一的非内置依赖项:
.service('ResultService', function(){
this.value = '';
this.inspections = '';
})
这是app.js:
angular.module('inspector', [
'inspector.search',
'inspector.results',
'inspector.location',
'inspector.services',
'ngRoute'
])
这是完整的错误:
angular.js:4630 Uncaught Error: [$injector:modulerr] Failed to instantiate
module inspector due to:
Error: [$injector:modulerr] Failed to instantiate module inspector.location
due to:
Error: [$injector:nomod] Module 'inspector.location' is not available! You
either
misspelled the module name or forgot to load it. If registering a module
ensure
that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.5.6/$injector/nomod?p0=inspector.location
at https://ancient-sea-93514.herokuapp.com/scripts/angular/angular.js:68:12
. . . .
**编辑所做的更改。
新版本的LocationDisplayController:
angular.module('inspector.location', [])
.controller('LocationDisplayController', ['ResultService',
function(ResultService) {
var controller = this;
controller.inspections = ResultService.inspections;
}]);
LocationDisplayController.$inject = ['ResultService']
最佳答案
您需要使用数组语法指定依赖项,例如:
angular.module('inspector.location', [])
.controller('LocationDisplayController', ['$scope', '$http', '$routeParams',
'ResultService',
function($scope, $http, $routeParams, ResultService) {
var controller = this;
this.inspections = ResultService.inspections;
}
]);
或显式使用注入(inject)器,以便 Angular 知道缩小后要注入(inject)什么。
关于javascript - Grunt uglify 后,Angular Controller 模块无法实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37506524/