0

Below are two different implementations of a controller in Angularjs. In the first type the author wrapped the controller function within square braces and defined two parameters before the function and passed them into the function(like array).

 myapp.controller('axajCtrl',['$http','$scope',function($http,$scope){
    $http.get('extras/data.json').success(function(response){ //make a get request to mock json file.
        $scope.data = response; //Assign data received to $scope.data
    })
    .error(function(err){
        //handle error
    })
}])

In the second type after the controller name there is a function that implements the controller functionality.

myapp.controller('axajCtrl',function($http,$scope){
    $http.get('extras/data.json').success(function(response){ //make a get request to mock json file.
        $scope.data = response; //Assign data received to $scope.data
    })
    .error(function(err){
        //handle error
    })
});

I am confused with the first type of implementation. Why is the function defined like an array. Does the first implementation have any advantages over second one?

1

1 Answer 1

0

The square brackets are optional when developing and compiling your JS file locally...

When in a production environment, i would say they are necessary as some minifiers and uglifiers rename the values inside the () which will screw up all your injections and dependencies.. The squares will not get renamed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.