diff --git a/docs/content/error/ng/btstrpd.ngdoc b/docs/content/error/ng/btstrpd.ngdoc new file mode 100644 index 000000000000..401e276720fa --- /dev/null +++ b/docs/content/error/ng/btstrpd.ngdoc @@ -0,0 +1,29 @@ +@ngdoc error +@name ng:btstrpd +@fullName App Already Bootstrapped with this Element +@description + +Occurs when calling angular.bootstrap on an element that has already been bootstrapped. + +This usually happens when you accidentally use both `ng-app` and `angular.bootstrap` to bootstrap an application. + +``` + +... + + + + +``` + +Note that for bootrapping purposes, the `` element is the same as `document`, so the following will also throw an error. +``` + +... + + +``` diff --git a/src/Angular.js b/src/Angular.js index 1a90f8bc0f98..90c7234a33bf 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1049,6 +1049,12 @@ function angularInit(element, bootstrap) { function bootstrap(element, modules) { var doBootstrap = function() { element = jqLite(element); + + if (element.injector()) { + var tag = (element[0] === document) ? 'document' : startingTag(element); + throw ngMinErr('btstrpd', "App Already Bootstrapped with this Element '{0}'", tag); + } + modules = modules || []; modules.unshift(['$provide', function($provide) { $provide.value('$rootElement', element); diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 56fc985ca6b6..f049c2fd0c05 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -656,6 +656,32 @@ describe('angular', function() { /\[\$injector:modulerr] Failed to instantiate module doesntexist due to:\n.*\[\$injector:nomod] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it\./ ); }); + + + it('should complain if an element has already been bootstrapped', function () { + var element = jqLite('
bootstrap me!
'); + angular.bootstrap(element); + + expect(function () { + angular.bootstrap(element); + }).toThrowMatching( + /\[ng:btstrpd\] App Already Bootstrapped with this Element '
'/i + ); + + dealoc(element); + }); + + + it('should complain if manually bootstrapping a document whose element has already been bootstrapped', function () { + angular.bootstrap(document.getElementsByTagName('html')[0]); + expect(function () { + angular.bootstrap(document); + }).toThrowMatching( + /\[ng:btstrpd\] App Already Bootstrapped with this Element 'document'/i + ); + + dealoc(document); + }) });