0

This directive doesn't work on server after minification. It gives the following error:

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

1 Answer 1

0

Parameter name shrinking done during minification breaks AngularJS's dependency injection. For example, "$compile" might become "d". You have to use the alternative syntax where the names of the services are specified as strings. Instead of the second argument passed to app.directive being a function, it's an array where the final element is that function and all the prior elements are the service names in the same order they appear in the function's param list.

Here's an example with an extra dependency added so you can see how the order matters.

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()
                  }
              });
          }
      };
    }
  ]
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.