How to fix AngularJs minify Error: [$injector:unpr]

By FoxLearn 2/16/2024 7:26:25 AM   97
This post show you how to fix AngularJs minify Error: [$injector:unpr]

Minify javascript files help you reduce download file size and load your site faster.

If you got an error 'Uncaught Error: [$injector:unpr]' with angularjs after deployment, you can fix the following.

(function () {
    'use strict';

    var app = angular.module('app', [
        // Angular modules 
        'ngRoute', 'ui.bootstrap'
    ]);

    app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {

    }]);
})();

instead of writing

(function () {
    'use strict';

    var app = angular.module('app', [
        // Angular modules 
        'ngRoute', 'ui.bootstrap'
    ]);

    app.config(function ($routeProvider, $locationProvider) {

    });
})();

Try to add '$routeProvider', '$locationProvider' to your function as a name, similar $scope, $http...etc in controller, service, factory, directive or you can use $inject as shown below.

(function () {
    'use strict';

    angular
        .module('app')
        .service('accountService', accountService);

    accountService.$inject = ['$http'];

    function accountService($http) {

    }
})();

When minifying your code, it will minify all code by replacing the variable name

controller : function($scope) {}

minified

controller : function(e) {}

You should use

controller : ["$scope", function($scope) { ... }]

or using $inject

accountController.$inject = ['$scope', '$uibModal', 'toaster', 'companyService', 'accountService'];