Thursday, July 7, 2016

Prevent minification to manipulate Angular JS controller parameters

In AngularJS controller can have $scope or $http parameters. These must be $scope or $http rather than s or h. Minification process removes extra spaces and convert JavaScript method parameters to single character so that it tries to convert them in single character parameters.
When we declare a controller using the module then most of us just simply specify the controller method as below:
    var app = angular.module("githubViewer", []);   
    //Controller
    var MainController = function($scope, $http) {   
    };   
    app.controller("MainController", MainController); //MainController is controller method
During the learning, I get to know that during the minification process these parameters are changes to some single character. If these are renamed then AngularJS will not able to find the $scope and $http parameter. It expect them as 6 character $scope and 5 character $http in the controller method. To avoid this happen you need manually specify the parameters name when you specify the controller name and controller method to the module.

app.controller("MainController", ["$scope", "$http", MainController]);
Hope this help someone to avoid making this mistake..

No comments :

Post a Comment