0

I'm quite new to AngularJs. In initial chapters itself I see $scope being used, like inside a controller.

<script>
var app = angular.module('app1', []);
app.controller('ctrl1', function($scope) {
    $scope.fn = "John";
    $scope.ln = "Parker";
});
</script>

If I replace the function parameter $scope with $s or so, then the code doesn't work. Why is that?

I mean it looks like we are passing a callback function or so, then why does the parameter name matter?

Please help.

Thanks in advance
Balu

1
  • As $scope is an angular variable. you can't change it with another name. Commented Feb 7, 2018 at 6:15

3 Answers 3

1

You can rename the $scope, but you will need to add some string mapping (so angularjs will figure out how to inject the correct item). This feature is used mainly on minification:

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

app.controller('ctrl1', ['$scope', function($s) {
    $s.fn = "John";
    $s.ln = "Parker";
}]);

JSFIDDLE.

By the way, it's better to stop using $scope and start using the controlleras functionality. Read about it here.

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

app.controller('ctrl1', function() {
    this.fn = "John";
    this.ln = "Parker";
});

<div ng-app="app1" ng-controller="ctrl1 as vm">
   {{vm.fn}}
</div>

JSFIDDLE.

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

3 Comments

This is something that I do not understand. Like I have some Java programming background. Over there, the parameter name doesn't matter at all when passed as callbacks. As I know, in Javascript also it is the same. So why do we need a mapping? I also had the same question for JQuery where $ is used as shortcut or so. So we have to use noConflict to change it. Someone gave a me a neat explaination, although I don't remember it completely now.
JavaScript is not a strongly typed framework and therefore, in order to achieve dependency injection, it will need you to pass strings as unique identifiers. In java, DI could be achieve via type mappings, attributes etc.
Thanks. I think I understand it now.
1

Scope is an object that refers to the application model. It is an execution context for expressions. Scopes are arranged in hierarchical structure which mimic the DOM structure of the application. Scopes can watch expressions and propagate events.

$scope is object provided by AngularJS, it is the binding part between the HTML (view) and the controller, and you can't change it the way you are expecting.

I suggest you please read the manual

1 Comment

Thanks. I guess it'll start making sense to me after I go in deep a bit more.
1

Angular recompiles the code and do dependency injection on the basis of demand. However a better practice is to write controller in this way :

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

This code will work even with obfuscation of file. You can try following link to have a better understanding of this.

Understanding dependency injection

2 Comments

Thanks, I'll go through the link.
Yes, angular uses possibly toString method on ctrl functions definition to find out all dependency list and inject tyem accordingly

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.