1

I have below AngularJS controller code

(function() {
    'use strict';
    angular
      .module('app')
      .controller('TemplateCtrl', TemplateCtrl);

      function TemplateCtrl($http, $auth, $rootScope,$scope){

      }
})();

After compression from http://jscompress.com/ I got below output.

!function(){"use strict";function t(t,l,n,e){}angular.module("app").controller("TemplateCtrl",t)}();

Before compression there was no error but after compression I am getting below error

 Error: [$injector:unpr] Unknown provider: tProvider <- t <- TemplateCtrl

enter image description here

I am not finding any clue to fix this ?

Thanks for your help and time.

1

4 Answers 4

2

For angular compressing you need to do some extra stuff. You need to let it know how to compress dependencies. So you need this:

(function() {
'use strict';
angular
  .module('app')
  .controller('TemplateCtrl', ["$http", "$auth", "$rootscope", "$scope", TemplateCtrl]);

  function TemplateCtrl($http, $auth, $rootScope,$scope){

  }
})();
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting same error after and before compression, after using this code.
0

Angular resolves the dependency based on the names.

Read more here: https://stackoverflow.com/a/35336414/2405040 (Dependency Annotation)

And change your code like this:

(function() {
    'use strict';
    angular
      .module('app')
      .controller('TemplateCtrl', ["$http", "$auth", "$rootscope", "$scope", TemplateCtrl]);

      function TemplateCtrl($http, $auth, $rootScope,$scope){

      }
})();

To prevent missing things for minification and write code using inline annotation syntax, add the ng-strict-di annotation with ng-app attribute.

Comments

0

Below code worked for me.

(function() {
'use strict';
angular
  .module('app')
  .controller('TemplateCtrl', TemplateCtrl);
  TemplateCtrl.$inject  = ['$http', '$auth', '$rootScope', '$scope'];

  function TemplateCtrl($http, $auth, $rootScope,$scope){

  }
})();

After compression this is

!function(){"use strict";function t(t,o,e,c){}angular.module("app").controller("TemplateCtrl",t),t.$inject=["$http","$auth","$rootScope","$scope"]}();

Thanks to all.

Comments

-2

wright you controller in this way :

angular
  .module('app')
  .controller('TemplateCtrl', function () {

 var something = function ($http, $auth, $rootScope,$scope){

  }
});

3 Comments

when you minify this your parameters will be like "t,l,n,e" this
@otololua Yes but the controller is not defined properly so he is getting that error
your function is just anonymous function and it doesn't matter

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.