From 0d60f8d367e38224696749b0f7de04bd60649815 Mon Sep 17 00:00:00 2001 From: Siddique Hameed Date: Fri, 17 Jan 2014 12:17:22 -0600 Subject: [PATCH] fix(angular.bootstrap): only allow angular to load once This is hard to test as a unit-test, since it involves the actual loading of angular, but it turns out that it is easy to test using a protractor e2e test. Closes #5863 Closes #5587 --- src/Angular.js | 35 +++++++++++++++++++++++++++++++++++ src/angular.suffix | 6 ++++++ 2 files changed, 41 insertions(+) diff --git a/src/Angular.js b/src/Angular.js index 96df13f4ff8d..ec69cc1a59a1 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1241,6 +1241,41 @@ function angularInit(element, bootstrap) { * Note that ngScenario-based end-to-end tests cannot use this function to bootstrap manually. * They must use {@link ng.directive:ngApp ngApp}. * + * Angular will detect if it has been loaded into the browser more than once and only allow the + * first loaded script to be bootstrapped and will report a warning to the browser console for + * each of the subsequent scripts. This prevents strange results in applications, where otherwise + * multiple instances of Angular try to work on the DOM. + * + * + * + * + *
+ * + * + * + * + * + * + * + *
{{heading}}
{{fill}}
+ *
+ *
+ * + * var app = angular.module('multi-bootstrap', []) + * + * .controller('BrokenTable', function($scope) { + * $scope.headings = ['One', 'Two', 'Three']; + * $scope.fillings = [[1, 2, 3], ['A', 'B', 'C'], [7, 8, 9]]; + * }); + * + * + * it('should only insert one table cell for each item in $scope.fillings', function() { + * expect(element.all(by.css('td')).count()) + * .toBe(9); + * }); + * + *
+ * * @param {Element} element DOM element which is the root of angular application. * @param {Array=} modules an array of modules to load into the application. * Each item in the array should be the name of a predefined module or a (DI annotated) diff --git a/src/angular.suffix b/src/angular.suffix index c86200bb31f4..9429d4fcf3d2 100644 --- a/src/angular.suffix +++ b/src/angular.suffix @@ -1,3 +1,9 @@ + if (window.angular.bootstrap) { + //AngularJS is already loaded, so we can return here... + console.log('WARNING: Tried to load angular more than once.'); + return; + } + //try to bind to jquery now so that one can write angular.element().read() //but we will rebind on bootstrap again. bindJQuery();