【问题标题】:Angular JS directive doesn't work on server after minificationAngular JS 指令在缩小后在服务器上不起作用
【发布时间】:2021-08-27 11:01:43
【问题描述】:

该指令在缩小后在服务器上不起作用。它给出了以下错误:

Error: [$injector:unpr] http://errors.angularjs.org/1.7.5/$injector/unpr?p0=eProvider%20%3C-%20e%20%3C-%20gwsRegexDirective
    at angular.js:99
    at angular.js:4905
    at Object.d [as get] (angular.js:5065)
    at angular.js:4910
    at d (angular.js:5065)
    at e (angular.js:5090)
    at Object.invoke (angular.js:5114)
    at angular.js:8756
    at r (angular.js:387)
    at Object.<anonymous> (angular.js:8754)
     app.directive('gwsRegex', function ($compile) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs, ngModelCtrl) {
                element.bind('keyup', () => {
                    let _regex = new RegExp(attrs['gwsRegex']);
                    var _value = element[0].value;
                    if (_value.match(_regex)) {
                        element[0].value = _value.replace(_value.match(_regex)[0], '').trim()
                    }
                });
            }
        };
    });

【问题讨论】:

标签: javascript angularjs angularjs-directive minify


【解决方案1】:

在缩小过程中缩小参数名称会破坏 AngularJS 的依赖注入。例如,“$compile”可能会变成“d”。您必须使用将服务名称指定为字符串的替代语法。不是传递给app.directive 的第二个参数是一个函数,而是一个数组,其中最后一个元素是该函数,所有前面的元素都是服务名称,其顺序与它们在函数的参数列表中出现的顺序相同。

这是一个添加了额外依赖项的示例,因此您可以了解顺序的重要性。

app.directive('gwsRegex', 
  [
    '$compile', '$timeout', 
    function ($compile, $timeout) {
      return {
          restrict: 'A',
          link: function (scope, element, attrs, ngModelCtrl) {
              element.bind('keyup', () => {
                  let _regex = new RegExp(attrs['gwsRegex']);
                  var _value = element[0].value;
                  if (_value.match(_regex)) {
                      element[0].value = _value.replace(_value.match(_regex)[0], '').trim()
                  }
              });
          }
      };
    }
  ]
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2018-06-08
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多