What is the difference between $scope and scope?

Technology CommunityCategory: AngularJSWhat is the difference between $scope and scope?
VietMX Staff asked 3 years ago

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

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.