Angularjs通过字符串名称将提供者注入模块工厂函数以进行缩小

bit*_*wit 20 javascript angularjs

我有以下代码:

appModule = angular.module('appModule', []);

appModule.factory('sharedApplication', function($rootScope, $http) {
  var sharedApp;
  sharedApp = {};
  sharedApp.currentView = "home-section";
  sharedApp.pastEvents = null;
  $http.get('api/highlights').success(function(response) {
    return sharedApp.pastEvents = response.data;
  });
  return sharedApp;
});
Run Code Online (Sandbox Code Playgroud)

这段代码完美无缺,正如我所料,直到我尝试缩小我的javascript,然后我得到

    Error: Unknown provider: eProvider <- e
Run Code Online (Sandbox Code Playgroud)

这是因为我的工厂函数中的$ http参数已重命名为'e'以进行缩小.那么如何手动通知appModule按名称注入什么以避免缩小我的代码?

提前致谢.

kfi*_*fis 44

尝试

appModule.factory('sharedApplication', ['$rootScope','$http',function($rootScope, $http) {

}]);
Run Code Online (Sandbox Code Playgroud)

问候

  • 由于这个原因,我100%使用数组语法 - IMO上面提到的其他语法应该被弃用.谁不会在某些时候缩小他们的代码? (3认同)
  • IMO,minification-safe语法应该是Angular文档中所有示例的默认语法. (2认同)