Vijey Vijey - 3 months ago 14
AngularJS Question

Angularjs: $scope vs scope

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?

Answer

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.

Comments