警告:试图装入不止一次因为jQuery ...更多...角度为什么呢? [英] WARNING: Tried to load angular more than once...because of jQuery...why?

查看:252
本文介绍了警告:试图装入不止一次因为jQuery ...更多...角度为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明白是怎么回事。该警告是自我解释,我意识到,在应用程序,使用code和结构,我有以下时,它会运行NG-观点两次(测试将两次在控制台登录,所以当然角是装两次!)......但是为什么呢?

I'm trying to understand what's going on here. The warning is self explanatory, and I realize that in the app, with the code and structure I have below, it runs the ng-view twice ('test' will be logged twice in the console, so of course angular is loaded twice!)....but why?

我读过每一个岗位,我能找到它,它似乎归结为jQuery的前角加载。

I've read every post I could find about it, and it seems to boil down to jQuery being loaded before angular.

如果我离开了jQuery的还是我angualr后加载的jQuery(这是不好的做法,从我的理解),没问题。我想有jQuery来支持某些功能(特别是 UI可排序)。而且,虽然它似乎没有被实际造成任何问题,我想没有它运行我的NG-观点的两倍。

If I leave out jQuery or if I load jQuery after angualr (which isn't good practice from what I understand), no problem. I'd like to have jQuery to support some functionality (specifically ui-sortable). And, although it doesn't seem to be actually causing any problems, I'd like to not have it running my ng-view twice.

我做得不对结构,还是我失去了一个明显的方式来解决这个问题?

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

更新:问题的 Plunker(检查控制台)

Update: Plunker of the issue (check the 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:
我的简单内容

simple.html: My simple content

推荐答案

好吧,总结从注释的帮助:

Ok, to summarize the help from the comments:

如果jQuery是包含任何&LT; 剧本>标记<一个href=\"http://stackoverflow.com/questions/18220197/angularjs-does-not-load-scripts-within-ng-view/18220411#18220411\">included内的NG-视图将被计算两次的。 (感谢@lossleader!)。

If jQuery is included, any <script> tags included WITHIN an ng-view will be evaluated twice. (Thanks @lossleader!).

我在测试中不正确的假设是,它处理整个模板内容两次,当我试图动过身体NG-视图到&LT; DIV >因为我看到了日志消息的两倍。这是不是!

My incorrect assumption in testing was that it was processing the entire template content twice when I tried moving the ng-view off the body onto a <div> because I saw the log message twice. It wasn't!

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

所以@汤姆和@Wawy两个有正确的解决方案。无论是移动NG-视图成&LT; DIV >或移动的&lt; 剧本>标签进入的&lt; >(外在NG-视图)。

So @Tom and @Wawy both had correct solutions. Either move the ng-view into a <div> or move the <script> tags into the <head> (outside of the ng-view).

这篇关于警告:试图装入不止一次因为jQuery ...更多...角度为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆