Accidentally loading angularjs multiple times causes strange behavior

spirographer

Due to dynamically loaded instances of Angular.js, I'm seeing an issue where ng-repeat will cube the number of items displayed.

The following code demonstrates the issue:

<html ng-app="brokentable">
<head>
    <title>Broken Angular Table</title>

  <!-- the following are dynamically added by other code -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>

   <script type="text/javascript">
        var app = angular.module('brokentable', []);

        app.controller('BrokenTable', function($scope) {
            $scope.headings = ['One', 'Two', 'Three'];
            $scope.fillings = [[1, 2, 3], ['A', 'B', 'C'], [7, 8, 9]];
        });
  </script>
</head>
<body ng-controller="BrokenTable">
    <table>
    <tr>
      <th ng-repeat="heading in headings">{{heading}}</th>
    </tr>
    <tr ng-repeat="filling in fillings">
      <td ng-repeat="fill in filling">{{fill}}</td>
    </tr>
  </table>
</body>
</html>

How do I prevent Angular's JS file from being loaded multiple times, but still enable dynamically adding script references to the page?

Given that I still have the need to add the script file references to my page dynamically, what can I do to keep the issue demonstrated above from happening?

spirographer

It is possible to wrap your loading code for angularjs using window.angular as follows:

  <script>
    if (!window.angular) {
      document.write('<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"><\/script>');
    }
  </script>

or if you happen to be using a local copy of angular:

  <script>
    if (!window.angular) {
      document.write('<script src="angular.js"><\/script>');
    }
  </script>

This way you can avoid inadvertently loading angular twice which results in unexpected and silent side effects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angularjs - Same images loading multiple times

strange angularJs variable behavior

Strange AngularJS beginner behavior

Strange behavior using multiple transclude component in Angularjs 1.6.1

Loading in a file multiple times (n) in a row causes my Message Box messages to show up (n) times

Putting a Hashmap into itself causes strange behavior

Multiple checkboxes with Ajax strange behavior

AngularJs Multiple Times loaded

Loading the same image multiple times

Authentication strange behavior with JWT, CakePHP 3 and AngularJS

angularJS and ng-model strange behavior

Trying to grep a find -print0 causes strange behavior

argc, argv causes strange behavior in Null terminator in C

VBA code for moving completed rows to another sheet causes strange behavior

Changing the value of a css variable from JS causes strange behavior

css --variable strange behavior whith multiple use

Cmd+backtick strange behavior with multiple spaces

Issue with Multiple Reactive Filters and Updateselectinputs - Strange Behavior

Strange Safari behavior with multiple SVG <animate>'s

Strange behavior creating multiple StreamReader on the same Stream

Concat causes stack overflow if called multiple times

What causes jqgrid events to fire multiple times?

Strange behavior when loading assemblies and its dependencies programatically

Django database strange behavior - dev server is loading about minutes

int works fine, but causes some jerky motion... Number causes strange behavior in AS3

AngularJS resources - loading multiple resources

Mistakenly loading the same images multiple times

Ember loading template multiple times and error

Stop function from loading items multiple times