feat($rootElement): added application root element

Publish the application root element as $rootElement
so that it can be injected to other services.
This commit is contained in:
Misko Hevery
2012-05-18 14:53:11 -07:00
parent 0a6e464a93
commit 85632cb44c
5 changed files with 47 additions and 3 deletions

View File

@@ -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);

View File

@@ -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()`.
*/

View File

@@ -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
@@ -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() {
@@ -1370,7 +1380,6 @@ angular.module('ngMock', ['ng']).provider({
});
/**
* @ngdoc overview
* @name angular.module.ngMockE2E

View File

@@ -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);
});
});

View File

@@ -950,6 +950,13 @@ describe('ngMock', function() {
});
});
});
describe('$rootElement', function() {
it('should create mock application root', inject(function($rootElement) {
expect($rootElement.text()).toEqual('');
}));
});
});