Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat($rootElement): added application root element
Browse files Browse the repository at this point in the history
Publish the application root element as $rootElement
so that it can be injected to other services.
  • Loading branch information
mhevery committed Jun 2, 2012
1 parent 0a6e464 commit 85632cb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,10 +913,13 @@ function angularInit(element, bootstrap) {
function bootstrap(element, modules) {
element = jqLite(element);
modules = modules || [];
modules.unshift(['$provide', function($provide) {
$provide.value('$rootElement', element);
}]);
modules.unshift('ng');
var injector = createInjector(modules);
injector.invoke(
['$rootScope', '$compile', '$injector', function(scope, compile, injector){
['$rootScope', '$rootElement', '$compile', '$injector', function(scope, element, compile, injector){
scope.$apply(function() {
element.data('$injector', injector);
compile(element)(scope);
Expand Down
13 changes: 13 additions & 0 deletions src/ng/rootElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

/**
* @ngdoc overview
* @name angular.module.ng.$rootElement
*
* @description
* The root element of Angular application. This is either the element where {@link
* angular.module.ng.$compileProvider.directive.ngApp ngApp} was declared or the element passed into
* {@link angular.bootstrap}. The element represent the root element of application. It is also the
* location where the applications {@link angular.module.AUTO.$injector $injector} service gets
* published, it can be retrieved using `$rootElement.injector()`.
*/
13 changes: 11 additions & 2 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,15 @@ function MockXhr() {
* Flushes the queue of pending tasks.
*/

/**
*
*/
angular.mock.$RootElementProvider = function() {
this.$get = function() {
return angular.element('<div ng-app></div>');
}
};

/**
* @ngdoc overview
* @name angular.module.ngMock
Expand All @@ -1359,7 +1368,8 @@ angular.module('ngMock', ['ng']).provider({
$browser: angular.mock.$BrowserProvider,
$exceptionHandler: angular.mock.$ExceptionHandlerProvider,
$log: angular.mock.$LogProvider,
$httpBackend: angular.mock.$HttpBackendProvider
$httpBackend: angular.mock.$HttpBackendProvider,
$rootElement: angular.mock.$RootElementProvider
}).config(function($provide) {
$provide.decorator('$timeout', function($delegate, $browser) {
$delegate.flush = function() {
Expand All @@ -1370,7 +1380,6 @@ angular.module('ngMock', ['ng']).provider({
});



/**
* @ngdoc overview
* @name angular.module.ngMockE2E
Expand Down
12 changes: 12 additions & 0 deletions test/ng/rootElementSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

describe('$rootElement', function() {
it('should publish the bootstrap element into $rootElement', function() {
var element = jqLite('<div></div>');
var injector = angular.bootstrap(element);

expect(injector.get('$rootElement')[0]).toBe(element[0]);

dealoc(element);
});
});
7 changes: 7 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,13 @@ describe('ngMock', function() {
});
});
});


describe('$rootElement', function() {
it('should create mock application root', inject(function($rootElement) {
expect($rootElement.text()).toEqual('');
}));
});
});


Expand Down

0 comments on commit 85632cb

Please sign in to comment.