2

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.

2 Answers 2

1

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

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

Comments

0

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){
}])

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.