angular 再通过 load 载入的页面 入口模块无法识别?

接手的之前的项目。一个左右分栏的效果,在index.html里面,通过load 方法 载入各个分页面。
现在我想在其中一个子页面里使用angular,在初始化模块的时候angular总是报错找不到入口模块。各位大神能不能帮我找下原因?

分页面代码

<html>
<body class="wysihtml5-supported">
  <section class="content-header" ng-app="myApp">
    <h1 ng-controller="Aaa">
      {{name}}
      <small></small>
    </h1>
  </section>

  <script src="angular.js"></script>
  <script>
      angular.module("myApp",[]).controller("Aaa",['$scope',function($scope){
          $scope.name = "出来好吧";
      }])
  </script>
</body>
</html>

报错信息如下
图片描述

页面如下
图片描述

求助怎么才能在这种情况下使用angular?

阅读 4.6k
3 个回答

主页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="app.css">
</head>
<body>
<div>用户信息</div>
<div id="angular-content"></div>

<script src="jquery.js"></script>
<script src="angular.js"></script>
<script>
        
    var $container = $('#angular-content');

    $container.load('/angular/load.html', function () {
        angular.module("myApp",[]).controller("Aaa",['$scope',function($scope){
            $scope.name = "出来好吧";
        }]);
        angular.bootstrap($container,['myApp'])
    });

</script>
</body>
</html>

load的页面

<section class="content-header">
    <h1 ng-controller="Aaa">
        {{name}}
        <small></small>
    </h1>
</section>

你load的页面不要加什么html、body标签,没有意义的。这里采用动态加载的方式,把相应的js放在主页面里。

<html>
<body class="wysihtml5-supported">
  <section class="content-header" ng-app="myApp">
    <h1 ng-controller="Aaa">
      {{name}}
      <small></small>
    </h1>
  </section>

  <script src="angular.js"></script>
  <script>
      angular.module("myApp",[]).controller("Aaa",function($scope){
          $scope.name = "出来好吧";
      })
  </script>
</body>

把两个<script></script>标签放在<head></head>里试试。写法上好像没什么问题,但是错误确实说的是你没声明myApp模块。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题