如何引用 angular 值,以便引用不会在缩小时被破坏

how to reference an angular value so that reference does not get destroyed on minification

以下代码在 minifiaction 之前工作正常。本质上,editableOptions 是由 angular-xeditable 库提供的 angular 值。

angular.module('common').run(function(editableOptions) {
        editableOptions.theme = 'bs3';
    }
)

但是,当我缩小时,出现注入错误。我相信这是因为 editableOptions 越来越小了。我怎样才能以不同的方式引用它,以免发生这种情况?有什么方法可以从 angular.module('xeditable') 开始引用它吗?

Angular 团队在他们的一个教程中提到了这个问题的解决方案:

https://docs.angularjs.org/tutorial/step_05

A Note on Minification

Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.

We can overcome this problem by annotating the function with the names of the dependencies, provided as strings, which will not get minified.

使用定义注入对象的缩小安全方法:

angular.module('common').run(
   ['editableOptions', 
      function(editableOptions) {
          editableOptions.theme = 'bs3';
      }
   ]);

这是另一种避免缩小和丑化问题的样式:

function runModule(editableOptions) {
  editableOptions.theme = 'bs3';
}

runModule.$inject = ['editableOptions'];

angular.module('common').run(runModule);

Todd Motto Angular Style Guide

This aids with readability and reduces the volume of code "wrapped" inside the Angular framework