How to fix injector error after building Angular?

Before talking, I read about it, making recommendations, but still causing an error. Check out the short code:

function IndexController($scope, $route, $routeParams, $location){
  $scope.sfv = project.version.name;
}
angular.module("TkwebMobile", ['ngRoute', 'ngCookies'])
  .controller('IndexController', ['$scope', '$route', '$routeParams', '$location', IndexController]);

Only this error is saved. I use grunt for "uglify" and I also use "concat" to combine the codes into "lib". Even I use the "injection" recommended in the Angular documentation.

Uncaught Error: [$injector:modulerr] Failed to instantiate module TkwebMobile due to:
Error: [$injector:unpr] Unknown provider: a

Is this a grunt concat problem? (Soil-vno-CONCAT)

+4
source share
3 answers

This is because of your minimum, in particular, in order to minimize and mutilate variable names.

Angular determines what value is entered into your functions from the parameter name. For instance...

angular.factory('MyFactory', function($location) {...});

... , angular '$location', $location, .

javascript, , mangle, . ...

angular.factory('MyFactory', function(a) {...});

Angular , $location a. javascript, angular. .

- , angular .

angular.factory('MyFactory', ['$location', function(a) {...}]);

, - . , , , minifier , angular , .

, , - mangle minifier. , , , , , .

- ngMin, , -. imo, js.

, mangle grunt, ...

uglify: {
  options: {
    report: 'min',
    mangle: false
  }
}

ngAnnotate . . . (ngAnnotate - , ngMin)

+8

. , .. , , . ( , .)

, . , .

, :

:

angular.module('FootCtrl', []).controller('FooterController', function($scope) {
    $scope.footer = 'Copyright \u00A9 ' + new Date().getFullYear() + name;
});

Fixed

angular.module('FootCtrl', []).controller('FooterController', ["$scope", function($scope) {
    $scope.footer = 'Copyright \u00A9 ' + new Date().getFullYear() + name;
}]);

, , .

, . , , , , .

+1

, , , , , $inject , , angular , . , , IIFE, . .

:

(() {

})()

The above code works fine to minimize due to the automatic insertion of a semicolon, but it breaks after minimization, because the lack of a semicolon screw up. Therefore, after the correction, he looked lower.

Correctly:

(function () {

}) ();

This fixed my problem. Hope this helps someone Note: I am using grunt useminPrepare, usemin, copy, uglify and ngAnnotate.

0
source

All Articles