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

Commit 85632cb

Browse files
committed
feat($rootElement): added application root element
Publish the application root element as $rootElement so that it can be injected to other services.
1 parent 0a6e464 commit 85632cb

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

src/Angular.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,13 @@ function angularInit(element, bootstrap) {
913913
function bootstrap(element, modules) {
914914
element = jqLite(element);
915915
modules = modules || [];
916+
modules.unshift(['$provide', function($provide) {
917+
$provide.value('$rootElement', element);
918+
}]);
916919
modules.unshift('ng');
917920
var injector = createInjector(modules);
918921
injector.invoke(
919-
['$rootScope', '$compile', '$injector', function(scope, compile, injector){
922+
['$rootScope', '$rootElement', '$compile', '$injector', function(scope, element, compile, injector){
920923
scope.$apply(function() {
921924
element.data('$injector', injector);
922925
compile(element)(scope);

src/ng/rootElement.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
/**
4+
* @ngdoc overview
5+
* @name angular.module.ng.$rootElement
6+
*
7+
* @description
8+
* The root element of Angular application. This is either the element where {@link
9+
* angular.module.ng.$compileProvider.directive.ngApp ngApp} was declared or the element passed into
10+
* {@link angular.bootstrap}. The element represent the root element of application. It is also the
11+
* location where the applications {@link angular.module.AUTO.$injector $injector} service gets
12+
* published, it can be retrieved using `$rootElement.injector()`.
13+
*/

src/ngMock/angular-mocks.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,15 @@ function MockXhr() {
13471347
* Flushes the queue of pending tasks.
13481348
*/
13491349

1350+
/**
1351+
*
1352+
*/
1353+
angular.mock.$RootElementProvider = function() {
1354+
this.$get = function() {
1355+
return angular.element('<div ng-app></div>');
1356+
}
1357+
};
1358+
13501359
/**
13511360
* @ngdoc overview
13521361
* @name angular.module.ngMock
@@ -1359,7 +1368,8 @@ angular.module('ngMock', ['ng']).provider({
13591368
$browser: angular.mock.$BrowserProvider,
13601369
$exceptionHandler: angular.mock.$ExceptionHandlerProvider,
13611370
$log: angular.mock.$LogProvider,
1362-
$httpBackend: angular.mock.$HttpBackendProvider
1371+
$httpBackend: angular.mock.$HttpBackendProvider,
1372+
$rootElement: angular.mock.$RootElementProvider
13631373
}).config(function($provide) {
13641374
$provide.decorator('$timeout', function($delegate, $browser) {
13651375
$delegate.flush = function() {
@@ -1370,7 +1380,6 @@ angular.module('ngMock', ['ng']).provider({
13701380
});
13711381

13721382

1373-
13741383
/**
13751384
* @ngdoc overview
13761385
* @name angular.module.ngMockE2E

test/ng/rootElementSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
describe('$rootElement', function() {
4+
it('should publish the bootstrap element into $rootElement', function() {
5+
var element = jqLite('<div></div>');
6+
var injector = angular.bootstrap(element);
7+
8+
expect(injector.get('$rootElement')[0]).toBe(element[0]);
9+
10+
dealoc(element);
11+
});
12+
});

test/ngMock/angular-mocksSpec.js

+7
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,13 @@ describe('ngMock', function() {
950950
});
951951
});
952952
});
953+
954+
955+
describe('$rootElement', function() {
956+
it('should create mock application root', inject(function($rootElement) {
957+
expect($rootElement.text()).toEqual('');
958+
}));
959+
});
953960
});
954961

955962

0 commit comments

Comments
 (0)