top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between $scope and scope?

0 votes
506 views
What is the difference between $scope and scope?
posted Oct 9, 2017 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The module factory methods like controller, directive, factory, filter, service, animation, config and run
receive arguments through dependency injection (DI). In case of DI, you inject the scope object with the dollar
prefix i.e. $scope. The reason is the injected arguments must match to the names of injectable objects followed
by dollar ($) prefix.
For example, you can inject the scope and element objects into a controller as given below:

module.controller('MyController', function ($scope, $element) { // injected arguments
});

When the methods like directive linker function don’t receive arguments through dependency injection, you just
pass the scope object without using dollar prefix i.e. scope. The reason is the passing arguments are received by
its caller.

module.directive('myDirective', function () // injected arguments here
{
 return {
 // linker function does not use dependency injection
 link: function (scope, el, attrs) {
 // the calling function will passes the three arguments to the linker: scope,
element and attributes, in the same order
 }
 };
});

In the case of non-dependency injected arguments, you can give the name of injected objects as you wish. The
above code can be re-written as:

module.directive("myDirective", function () {
 return {
 link: function (s, e, a) {
 // s == scope
 // e == element
 // a == attributes
 }
 };
});

In short, in case of DI the scope object is received as $scope while in case of non-DI scope object is received as
scope or with any name.

answer Oct 9, 2017 by Shivaranjini
...