mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
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:
parent
0a6e464a93
commit
85632cb44c
5 changed files with 47 additions and 3 deletions
|
|
@ -913,10 +913,13 @@ function angularInit(element, bootstrap) {
|
||||||
function bootstrap(element, modules) {
|
function bootstrap(element, modules) {
|
||||||
element = jqLite(element);
|
element = jqLite(element);
|
||||||
modules = modules || [];
|
modules = modules || [];
|
||||||
|
modules.unshift(['$provide', function($provide) {
|
||||||
|
$provide.value('$rootElement', element);
|
||||||
|
}]);
|
||||||
modules.unshift('ng');
|
modules.unshift('ng');
|
||||||
var injector = createInjector(modules);
|
var injector = createInjector(modules);
|
||||||
injector.invoke(
|
injector.invoke(
|
||||||
['$rootScope', '$compile', '$injector', function(scope, compile, injector){
|
['$rootScope', '$rootElement', '$compile', '$injector', function(scope, element, compile, injector){
|
||||||
scope.$apply(function() {
|
scope.$apply(function() {
|
||||||
element.data('$injector', injector);
|
element.data('$injector', injector);
|
||||||
compile(element)(scope);
|
compile(element)(scope);
|
||||||
|
|
|
||||||
|
|
@ -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
src/ngMock/angular-mocks.js
vendored
13
src/ngMock/angular-mocks.js
vendored
|
|
@ -1347,6 +1347,15 @@ function MockXhr() {
|
||||||
* Flushes the queue of pending tasks.
|
* Flushes the queue of pending tasks.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
angular.mock.$RootElementProvider = function() {
|
||||||
|
this.$get = function() {
|
||||||
|
return angular.element('<div ng-app></div>');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc overview
|
* @ngdoc overview
|
||||||
* @name angular.module.ngMock
|
* @name angular.module.ngMock
|
||||||
|
|
@ -1359,7 +1368,8 @@ angular.module('ngMock', ['ng']).provider({
|
||||||
$browser: angular.mock.$BrowserProvider,
|
$browser: angular.mock.$BrowserProvider,
|
||||||
$exceptionHandler: angular.mock.$ExceptionHandlerProvider,
|
$exceptionHandler: angular.mock.$ExceptionHandlerProvider,
|
||||||
$log: angular.mock.$LogProvider,
|
$log: angular.mock.$LogProvider,
|
||||||
$httpBackend: angular.mock.$HttpBackendProvider
|
$httpBackend: angular.mock.$HttpBackendProvider,
|
||||||
|
$rootElement: angular.mock.$RootElementProvider
|
||||||
}).config(function($provide) {
|
}).config(function($provide) {
|
||||||
$provide.decorator('$timeout', function($delegate, $browser) {
|
$provide.decorator('$timeout', function($delegate, $browser) {
|
||||||
$delegate.flush = function() {
|
$delegate.flush = function() {
|
||||||
|
|
@ -1370,7 +1380,6 @@ angular.module('ngMock', ['ng']).provider({
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc overview
|
* @ngdoc overview
|
||||||
* @name angular.module.ngMockE2E
|
* @name angular.module.ngMockE2E
|
||||||
|
|
|
||||||
12
test/ng/rootElementSpec.js
Normal file
12
test/ng/rootElementSpec.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
7
test/ngMock/angular-mocksSpec.js
vendored
7
test/ngMock/angular-mocksSpec.js
vendored
|
|
@ -950,6 +950,13 @@ describe('ngMock', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('$rootElement', function() {
|
||||||
|
it('should create mock application root', inject(function($rootElement) {
|
||||||
|
expect($rootElement.text()).toEqual('');
|
||||||
|
}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue