mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 22:45:52 +08: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:
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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
|
||||
|
||||
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('');
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user