jQuery.load包含AngularJS的HTML

时间:2017-04-27 12:17:47

标签: javascript jquery html angularjs

我在加载包含AngularJS代码的HTML文件时遇到问题!

以下是我的摘要:

page1.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1"></meta>   
    <script src="jquery-2.2.4.min.js"></script>
</head>
<body>        
    <div id="content">
    </div>
    <script>
        $(document).ready(function () {
            $('#content').load('page2.html#body');
        });        
    </script>
</body>
</html>

page2.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
    <script src="angular.min.js"></script>
</head>
<body>
    <div ng-app="myApp" ng-controller="customersCtrl">        
        {{ value }}
    </div>

    <script>
    //<![CDATA[
        var app = angular.module('myApp', []);  
        app.controller('customersCtrl', function($scope) {
            $scope.value = "hello world!";
        });    
    //]]>
    </script>

</body>
</html>

page2 单独工作就好了!但是当我尝试在 page1 中加载它时,我收到以下错误消息:

  

未捕获错误:[$ injector:modulerr] http://errors.angularjs.org/1.4.8/ $ injector / modulerr?p0 = myApp&amp; p1 =错误%3A%2 ...   在HTMLDocument.eval(eval at globalEval(jquery-2.2.4.min.js:2),:294:192)   at i(jquery-2.2.4.min.js:2)

1 个答案:

答案 0 :(得分:0)

Angular需要jQuery才能工作(确切地说,it needs jQLite )。您应该在angular.min.js之前的page2.html上导入它。

<head>
    <meta charset="utf-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
    <script src="jquery-2.2.4.min.js"></script>
    <script src="angular.min.js"></script>
</head>