WARNING: Tried to load angular more than once ... due to jQuery ... why?

I am trying to understand what is happening here. The warning is self-evident, and I understand that in an application with the code and structure that I have below, it runs ng-view twice ("test" will be registered twice in the console, so, of course, angular loads twice!) .... but why?

I read every post I could find about this, and it seems to load before jQuery loads to angular.

If I leave jQuery or load jQuery after angualr (which is not good practice from what I understand), no problem. I would like to have jQuery to support some functions (specifically ui-sortable ). And, although this does not actually cause any problems, I would like it to not execute my ng-view twice.

Am I doing something structurally wrong or am I missing an obvious way to fix this?

Update: Plunker problems (check console)

index.html

<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Site Title</title> </head> <body ng-view> <script type="text/javascript"> console.log('test') </script> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.js"></script> <script type="text/javascript" src="app_simple.js"></script> </body> </html> 

app_simple.js:

 'use strict'; /** * Configure client module */ var myApp = angular.module('myApp', [ 'ngRoute' ]); myApp.config(function ($routeProvider) { $routeProvider .when('/simple', { templateUrl: 'components/simple/simple.html', controller: 'SimpleCtrl' }) .otherwise({ redirectTo: '/x' }) }); myApp.controller('SimpleCtrl', ['$scope', '$log', '$http', function($scope, $log, $http){ }]); 

simple.html: My simple content

+6
source share
2 answers

Good to summarize the help from the comments:

If jQuery is enabled, any tags included in the ININ ng-view will be evaluated twice . (Thanks @lossleader!).

My wrong assumption in testing was that it processed all the template content twice when I tried to move the ng-view from the body to the <div>, because I had seen the log message twice. It was not!

 <body> <div ng-view> <script>console.log('duplicated if jQuery');</script> </div> </body> 

So, @Tom and @Wawy had the right solutions. Either move ng-view to <div> or move <script> tags to <head (outside of ng-view).

+8
source

I have the same problem and I fix it by loading jQuery before loading AngularJS hope this works for you

0
source

Source: https://habr.com/ru/post/978484/


All Articles