Shouvik
Shouvik

Reputation: 447

How angularjs DI is different from normal function arguments?

In angular if we define a controller then we write like

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

app.controller('myCtrl',function($scope){
})

Here $scope is a DI, which is passed inside a callback function as an function argument. Now, I understand that naming matters in angular here, but apart from that what is really the difference between a Normal function argument and Dependency injection in angular.

Upvotes: 2

Views: 46

Answers (2)

pcnate
pcnate

Reputation: 1774

You will notice that function($scope) {...} is the second argument to the .controller method of app. This is an anonymous function that gets executed when the controller is initialized. $scope is detected as a dependency by angular and passed into this controller function. Other than that, there isn't a difference. I generally don't think of the anonymous function as a function with arguments.

Another way to write that function is below. This is the more common manner to write as it will allow code minimizers to function properly. It tells angular what dependancies to inject in what order. The short circuit syntax you wrote also works.

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

app.controller('myCtrl', [ '$scope', function($scope){
}])

Upvotes: 0

Sohil
Sohil

Reputation: 562

Here's a video link that will clarify the concept of AngularJS variable and function.

Upvotes: 1

Related Questions