【问题标题】:AngularJs throws error after minifyAngularJs 缩小后抛出错误
【发布时间】:2016-10-21 17:47:48
【问题描述】:

缩小我的 angularjs 代码后,我得到一个错误:

无法实例化模块应用程序,原因是: 错误:[$injector:unpr] 未知提供者:t

代码只有在运行 gulp 没有 --production 标志 时才有效。

//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.
    }
})();

【问题讨论】:

  • 我现在很难提升东海岸 DDOS 的链接 bc,但请查看 ng-annotate - 它解决了缩小破坏 Angular 的问题。

标签: angularjs laravel gulp minify


【解决方案1】:

在注册模块之前尝试设置$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

【讨论】:

  • 我应该试试 ng-annotate ,即使这段代码也抛出了同样的错误。
  • 是的,最好使用严格模式来强制执行它。
  • @TamasHegedus 是的,我做到了,现在我试图让 ng-annonate 在 laravel-elixir 上工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 2013-04-07
  • 1970-01-01
  • 2015-05-26
  • 2013-03-24
  • 2015-11-07
相关资源
最近更新 更多