0

I have a simple angularjs directive and when I minify the file i get error because the variable names are changed

var app= angular.module('app',[]);

app.directive('directive',function(){
    return {
        restrict: 'EA',
        scope: {},
        replace: true,
        link: function($scope, element, attributes){
            $scope.name="test-test";
        ,
        controller: function($scope,$attrs,$http){

        },
        templateUrl: 'directives/app/app.tpl.html'
    }
});

the problem is $scope.name changes into a.name and angular doesn't recognize it. I tried injecting $scope by trying something like

link: ['$scope',function($scope, element, attributes){
            $scope.name="test-test";
}],
controller: ['$scope','$attrs','$http',function($scope,$attrs,$http){

}],

but I still get the same error of a.name when minified.

3
  • The link function doesn't use dependency injection. Is this actually the exact code you are testing? With your original code the link function should work fine and your controller should throw an error about unknown providers Commented Aug 22, 2016 at 18:32
  • yes this is the exact code (i am new to angularJS and playing around with it). It did throw the error before but after tried to inject it does not throw any error but the data binding does not work and I see a.name instead of $scope.name Commented Aug 22, 2016 at 18:34
  • Are you intentionally trying to use $scope in stead of scope in your link function, even though you have specified an isolated scope in your directive? Commented Aug 22, 2016 at 18:34

2 Answers 2

2

Directives link functions aren't injected. They're passed fixed set of parameters which are listed and comprehensively described in angular.js documentation. However this is not the case with controller. These are injected and should be annotated before minification. You can do it in at least 3 ways:

  • using array syntax like in your example
  • setting $inject property on controller function with value being array of names of injectables
  • annotating with ngAnnotate which will detect uses of angular injection and annotate it properly
Sign up to request clarification or add additional context in comments.

Comments

0

The link function is not dependency injected. It uses only positional arguments so you don't have to use that explicit array naming. The controller one is fine I think.

At last, remember, you must write code for clarity. The explicit syntax is a bit too verbose, that's why most people choose to use ng-annotate instead. Ng-annotate is a compilation step which will convert the code to the explicit form before minification.

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.