$scope
is neither a service nor an instance of $rootScope
. It is an instance of Scope
(which is a private constructor in Angular's code). In fact, the $rootScope
service's provider returns an instance of Scope
.
Let's start from the beginning:
Angular has a private (as in not directly accessible from outside, e.g. by you) constructor, called
Scope
.Scope
can be used by Angular to create new scope objects. (Indeed, all scopes, including isolate scopes and the rootScope itself, are internally created usingnew Scope()
.)Every scope object has a
$new()
method, which creates a new scope object that prototypically inherits from the scope whose$new()
method was called.As soon as Angular is loaded, it "requires" the
$rootScope
service, which creates the rootScope: a plain old Scope object (without a parent scope). Since services are singletons in Angular, anytime the$rootScope
service is injected, that same rootScope object is provided.Whenever a new scope is required (e.g. for a directive that defines a new scope (either normal or isolate), for a controller etc.), a new Scope object is created and returned using the aforementioned
$new()
method of an already existing Scope object (i.e. the rootScope or a descendant of the rootScope).So, what is the
$scope
dependency injected into the controllers ?
It is a Scope object, created by Angular's calling the enclosing scope's$new()
method and passed to the controller constructor. (So, it is not a service, but a Scope instance created for us by Angular.)
That said, your code could be changed like this:
var $injector = angular.element(document).injector();
$injector.get('$rootScope'); // it will return $rootScope
$injector.get('$rootScope').$new(); // it will return a new scope
// (prototypically inheriting from $rootScope)
// Or, if you have access to some other scope
// (example to the controller's $scope)
$scope.$new(); // it will return a new scope
// (prototypically inheriting from $scope)