One way to define a controller:
myApp.controller("TestController", ['$scope', function(bar) {
The whole reason angular works is related to how it injects dependencies. It basically searches for dependencies (e.g. $scope ) and tries to insert them based on the variable name. This means that if you call $scope something else, it will try to introduce another element instead. bar is not something that angular knows how to type so that you get the value undefined instead of the $scope you expected.
My example above works because I used an array to extract the variable names that will be inserted from the variables themselves. Now you do not need to use the word $scope , but you must keep the variables in the same order. The whole DI concept is too complicated to explain in a short article, so I recommend reading more about it on the angular website here: https://docs.angularjs.org/tutorial/step_05
DI point (in angular)
In case you are interested - why go to all these troubles? Why not just insert $scope into the first variable in the list? The answer is flexibility and customization. With angular, you can define your services and factories and then inject them into your controllers. This gives you the ability to define common procedures, objects, or even custom controls, and then use as many controllers as possible. Without DI, none of this would have been possible. Again, these are fairly complex concepts, so I recommend starting with the leap of faith that “it works” by learning about the services, and then you can come back and learn more about it. At this point, I think DI should make a lot more sense!
Good luck
source share