Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs: $scope vs scope

Tags:

angularjs

In Angularjs, is there a specific reason to use $scope in controllers and scope (without "$") in directives link function? Is it just a convention or anything else?

like image 476
Vijey Avatar asked Oct 10 '13 06:10

Vijey


People also ask

What is the scope of $scope in AngularJS?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

What is difference between scope and rootScope in AngularJS?

The main difference is the availability of the property assigned with the object. A property assigned with $scope cannot be used outside the controller in which it is defined whereas a property assigned with $rootScope can be used anywhere.

Is $scope still supported in angular2?

In Angular 2.0, there will be no $scope .

What is scope data in AngularJS?

AngularJS Scope The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.


2 Answers

The case when you do $scope in controller the Dependency Injection injects scope based on matching the variable name $scope, in this case using scope as name would not work.

For case of directive the injection is position based so you can name your variable a or b or any thing. The directive order for link function is

(scope, iElement, iAttrs, controller)

so first element is always scope object.

like image 147
Chandermani Avatar answered Oct 19 '22 08:10

Chandermani


You can find also a very good video answer to this question by John Lindquist here: http://egghead.io/lessons/angularjs-scope-vs-scope.

like image 33
Adam Bogdan Boczek Avatar answered Oct 19 '22 07:10

Adam Bogdan Boczek