Wednesday, September 10, 2014

AngularJS: Minification Issue

If you think about minification for all function, It may break your functionality.  Below is best example for controller.

var appModule = angular.module('appModule',[]);

appModule.controller('appController', function ($scope, $http) {
    $http.get('js/phones.json').success(function(data){
            $scope.phones = data;
        }
    );
    $scope.orderPro = 'age';
});

Minification will convert your arguments in variable. Below are two different solutions for this problem.

Solution #1

appModule.controller('appController',['$scope','$http',function($scope,$http){
 $http.get('js/phones.json').success(function(data)
 {
     $scope.phones = data;
 });
    $scope.orderPro='age';
}]);


Solution #2

function appController($scope,$http){
    $http.get('js/phones.json').success(function(data){
        $scope.phones = data;
    });
    $scope.orderPro='age';
}
appController.$inject= ['$scope','$http'];
appModule.controller('appController',appController);

No comments:

Post a Comment