Permalink
Browse files

feat($rootElement): added application root element

Publish the application root element as $rootElement
so that it can be injected to other services.
  • Loading branch information...
1 parent 0a6e464 commit 85632cb44c95617d73c369f3a03fb476a4d5c8a2 @mhevery mhevery committed May 18, 2012
Showing with 47 additions and 3 deletions.
  1. +4 −1 src/Angular.js
  2. +13 −0 src/ng/rootElement.js
  3. +11 −2 src/ngMock/angular-mocks.js
  4. +12 −0 test/ng/rootElementSpec.js
  5. +7 −0 test/ngMock/angular-mocksSpec.js
View
@@ -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
@@ -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()`.
+ */
@@ -1348,6 +1348,15 @@ function MockXhr() {
*/
/**
+ *
+ */
+angular.mock.$RootElementProvider = function() {
+ this.$get = function() {
+ return angular.element('<div ng-app></div>');
+ }
+};
+
+/**
* @ngdoc overview
* @name angular.module.ngMock
* @description
@@ -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
@@ -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);
+ });
+});
@@ -950,6 +950,13 @@ describe('ngMock', function() {
});
});
});
+
+
+ describe('$rootElement', function() {
+ it('should create mock application root', inject(function($rootElement) {
+ expect($rootElement.text()).toEqual('');
+ }));
+ });
});

0 comments on commit 85632cb

Please sign in to comment.