Mukund Kumar
Mukund Kumar

Reputation: 23211

What is the difference in minifying these two pieces of code?

i have the following non minfied and minified version code for controller:

non-minified version code :

phonecatApp.controller('PhoneListCtrl', function PhoneListCtrl($scope, $http) {
   //code for controller
});

minified version code :

phonecatApp.controller('PhoneListCtrl', ['$scope', '$http',function PhoneListCtrl($scope, $http) {
    //code for controller
}]);

i don't know why minified version code is preffered ? what is the difference between minified and non-minified version code ?

Upvotes: 0

Views: 85

Answers (1)

joemfb
joemfb

Reputation: 3056

Minification renames variables (among other things). Your first sample will no longer work when minified, as angular won't know what to inject when $scope and $http are renamed. Your second example, using the array syntax, tells angular what to inject regardless of the variable names.

(There are syntax errors in both of your examples: PhoneListCtrl($scope, $http) should be function PhoneListCtrl($scope, $http).)

Note: when using named functions as you are, there's another option for minification-safe angular code:

phonecatApp.controller('PhoneListCtrl', PhoneListCtrl);

PhoneListCtrl.$inject = ['$scope', '$http'];

function PhoneListCtrl($scope, $http) {
  //code for controller
}

Finally, if you really prefer your first example, you can use ng-annotate to pre-process your angular code and make it minification-safe.

Upvotes: 4

Related Questions