Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs minification issue with controller [closed]

Here's my controller:

angular.module('domiciliations').controller('mandatsCtrl', ['$scope', 'Domiciliation', 'logger', function ($scope, Domiciliation, logger) {
    $scope.mandats = Domiciliation.query();

    $scope.fullName = function () {
        return this.Person ? 'test' : 'test2'
    }

    $scope.isNextDisabled = function () {
        return false;
    }

    $scope.isPrevDisabled = function () {
        return true;
    }

    $scope.next = function () {
        logger.info("test");
    }
}]);

When minified, I get an error:

Error: Unknown provider: nProvider <- n

Now, if I include the following line after my controller, then it works:

mandatsCtrl.$inject = ['$scope', 'Domiciliation', 'logger'];

I thought the whole point of having the array type declaration in the controller function, was to avoid having to use $inject.... Did I miss something ?

like image 641
Sam Avatar asked Mar 24 '23 07:03

Sam


1 Answers

If you were to minify the JavaScript code for controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly. There are two ways to handle minification in angular

1)Just assign an array with service identifier strings into the $inject property of the controller function
 mandatsCtrl.$inject = ['$scope', 'Domiciliation', 'logger'];

2) Using bracketnotation
var mandatsCtrl= ['$scope', 'Domiciliation','logger' function($scope, $Domiciliation,logger) { /* constructor body */ }];

http://docs.angularjs.org/tutorial/step_05

like image 119
Ajay Beniwal Avatar answered Apr 11 '23 05:04

Ajay Beniwal