-3

Is the array necessary ?

   app.controller('myController', ['$scope', function($scope){

}])

Will this work like the code above?

   app.controller('myController', function($scope){

})
0

2 Answers 2

1

The array is always necessary, but it doesn't have to be combined into the controller definition. There is a much cleaner way to do it, and its my preferred method. What I do is create the controller in a functional manner. So for example:

app.controller('myController', MyController);

//dependency injection done here using the array of definitions
MyController.$inject = ['$scope'];

//all of the dependencies are added as parameters into the controller function
function MyController ($scope) {
   //insert controller code
}

As pointed out in the answer by Sajeetharan, this is called the explicit method. You should check out this style guide that is endorsed by the Angular 1 team for other patterns that help make the code cleaner and easier to maintain

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

Comments

1

Yes it will work in the same way, its just the difference.You need to use explicit dependency injection(second way).

Even when you minify it converts $scope to variable a and $http to variable b, their identity is still preserved in the strings.

1 Comment

Thanks! It was just a simple question cause I never used the explicit dependency injection

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.