在缩小我的angularjs代码后,我收到一个错误:
由于以下原因导致无法实例化模块应用: 错误:[$ injector:unpr]未知提供商:t
代码只有在没有 - 生产标志的情况下运行gulp 时才有效。
//All dependencies used in below are minified in a single file
(function(){
angular
.module('app', [
'ngMessages',
'ngStorage',
'ngAnimate',
'toastr',
'ngSanitize'
]);
})();
服务代码
(function(){
'use strict';
angular
.module('app')
.factory('dataservice', dataservice);
dataservice.$inject = ['$http','$localStorage','toastr'];
function dataservice($http,$localStorage,toastr) {
//Code inside this function uses all 3 dependencies injected
}
})();
控制器代码
(function(){
'use strict';
angular
.module('app')
.controller('LoginController',LoginController);
LoginController.$inject = ['$localStorage','dataservice','toastr','$scope','$sanitize'];
function LoginController($localStorage,dataservice,toastr,$scope,$sanitize) {
/// All 5 dependencies injected here are used inside this function.
}
})();
答案 0 :(得分:0)
在注册模块之前尝试设置$inject
属性:
(function(){
'use strict';
dataservice.$inject = ['$http','$localStorage','toastr'];
function dataservice($http,$localStorage,toastr) {
//Code inside this function uses all 3 dependencies injected
}
angular
.module('app')
.factory('dataservice', dataservice);
})();
或者你甚至可以在缩小之前添加ng-annotate
作为构建步骤来摆脱$inject
。