19

My Rails app doesn't work after turned on assets magnification when assets compiled. I converted the Angular controllers to use the bracket notation, and get following error, is there a way to debug this?

Compiled application.js https://gist.github.com/jianbo/6665161

JS ERROR

Error: Unknown provider: tProvider <- t
at Error (<anonymous>)
at me:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:817:21665
at Object.i [as get] (me:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:817:20671)
at me:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:817:21753
at i (localme:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:817:20671)
at n (me:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:817:20805)
at Object.r [as instantiate] (me:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:817:21447)
at me:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:818:604
at me:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:817:28889
at r (me:3001/assets/application-4f6cd4e170fc6ce5d181d869af318557.js:817:8277) application-4f6cd4e170fc6ce5d181d869af318557.js:818
(anonymous function) application-4f6cd4e170fc6ce5d181d869af318557.js:818
(anonymous function) application-4f6cd4e170fc6ce5d181d869af318557.js:818
r.$broadcast application-4f6cd4e170fc6ce5d181d869af318557.js:818
(anonymous function) application-4f6cd4e170fc6ce5d181d869af318557.js:818
l application-4f6cd4e170fc6ce5d181d869af318557.js:818
l application-4f6cd4e170fc6ce5d181d869af318557.js:818
(anonymous function) application-4f6cd4e170fc6ce5d181d869af318557.js:818
r.$eval application-4f6cd4e170fc6ce5d181d869af318557.js:818
r.$digest application-4f6cd4e170fc6ce5d181d869af318557.js:818
r.$apply application-4f6cd4e170fc6ce5d181d869af318557.js:818
r application-4f6cd4e170fc6ce5d181d869af318557.js:818
m application-4f6cd4e170fc6ce5d181d869af318557.js:818
v.onreadystatechange application-4f6cd4e170fc6ce5d181d869af318557.js:818
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
user1883793
  • 4,011
  • 11
  • 36
  • 65

4 Answers4

48

This error itself is Angular saying that it doesn't know what to inject for 't'. Which means that 't' must be a minified name for one of your injections somewhere.

If it works before minification but not after then it has to be an issue with something somewhere not using the min-safe injection method.

I'd check to make sure everything you're doing is minsafe, and also that you are not trying to minify the non-minsafe version of angular.js itself. Always use the .min that comes in the angular package rather than minifying your own (or if you do want to minify your own make sure it's the minsafe version).

Here's an example of making a controller minsafe. The following is NOT minsafe:

angular
    .module('myModule')
    .controller('MyCtrl', function($scope, myService) { 
        //your non-minsafe controller 
    });

To make it minsafe we encapsulate the function call in an array which begins with the things we want to inject, and ends in the function call with the same order of arguments:

angular
    .module('myModule')
    .controller('MyCtrl', ['$scope', 'myService', function($scope, myService) { 
        //your minsafe controller 
    }]);
Mike Driver
  • 8,481
  • 3
  • 36
  • 37
  • 1
    Hi Mike, I am using angularjs-rails gem which uses un-minifed angular.js https://github.com/hiravgandhi/angularjs-rails/blob/master/vendor/assets/javascripts/angular.js, could this be the reason? I should overwrite this with angular.min.js? Looks like the error is in line 2997.. Thanks – user1883793 Sep 23 '13 at 00:49
  • Check out the grunt-angular-templates plugin. – William Scott Sep 23 '13 at 01:09
  • @user1883793 There probably nothing wrong with the angular file. What Mike is saying is that angular's DI doesn't work with minification unless you use the second syntax he is describing. Angular literally parses the function header to determine what services to inject, and minification messes up those names. – mortalapeman Sep 23 '13 at 02:09
  • I have done what Mike said, and gone through my angular code several times, still cannot find the problem. Is there a better way to debug this? thanks – user1883793 Sep 23 '13 at 02:32
  • Hi - did you make sure you're not minifying the angular.js file itself? If you are this could cause an issue such as the one you describe. You should use the angular.min.js that comes with the angular download package instead - and NOT minify that file. – Mike Driver Sep 23 '13 at 09:42
  • Doing what @mike said fixed my issue. Thank you! – Ryan Angilly Oct 23 '13 at 22:47
  • I am using the same approach but still getting the same issue...What should I do now? – Mohd Anas Jun 18 '15 at 10:53
12

I have the same problem with the gem hiravgandhi/angularjs-rails

I was able to work OFF minifcation in production by changing my settings in config/environments/production.rb

config.assets.js_compressor = Uglifier.new(mangle: false)

Using a Rails 4.0.2 app with the hiravgandhi/angularjs-rails gem as instructed by the gem installation instructions.

Jason FB
  • 4,752
  • 3
  • 38
  • 69
8

I had a same problem and I found out that the problem was not in .controller call, it was in .config where it was not minification safe.

before

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

app.config(function(RestangularProvider) {
    RestangularProvider.setDefaultHeaders({'Content-Type': 'application/json'});
    RestangularProvider.setBaseUrl('http://myapi.com/api/v1');
});

After

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

app.config(['RestangularProvider', function(RestangularProvider) {
    RestangularProvider.setDefaultHeaders({'Content-Type': 'application/json'});
    RestangularProvider.setBaseUrl('http://myapi.com/api/v1');
}]);
Patrick Cho
  • 1,321
  • 1
  • 10
  • 8
0

I have similar issue with angular-blockUI module.. Guess i may have to use pre-minified vesion of that file as a separate stuff to be loaded instead of including into Web Essentials bundle. Some code we get via bower is not prepared for bundling & minification at all....

Konstantin Isaev
  • 642
  • 8
  • 14