如何使用附加到html标签的嵌套ng应用程序将一个基于angularjs的SPA包含到另一个基于angularjs

How to include one angularjs based SPA into another angularjs based SPA using nested ng-app which are attached to html tag?

本文关键字:angularjs 一个 另一个 SPA 包含 html 何使用 标签 嵌套 应用程序 ng      更新时间:2023-09-26

角度模块定义为:

angular.module('module1',['ngRoute']);

模块1的路由定义为

 module1.config(function($routeProvider) 
 {
      $routeProvider
      .when('/Application1',
       {
            templateUrl: 'Application1/Application1_HomePage.html',
       })         
      .otherwise({
          templateUrl: 'MainPage.html',
          controller: 'ctrl'
      )}
 });

控制器ctrl定义为:

 module1.controller('ctrl',function($scope,$location)
 {
         $scope.goToApp1 = function()
         {
              $location.path('/Application1',true);
         }
 });

外壳页面为:

<html ng-app="module1">
   <body>
      <div ng-view>   </div>                   
   </body>
</html>

Application1_HomePage.html包含另一个ng应用程序,而不是shell页面ng应用程序模块1

从上面显示的shell页面中,当通过routeProvider调用Application1_HomePage.html时,在Application1_HomePage.html中编写的ng应用程序的初始化失败。浏览器检查器清楚地显示DOM之外的任何内容。

考虑到以上场景,我如何从一个ng应用程序导航到另一个ng程序?

假设两个模块都有一个入口页面,您只需放置一个指向另一个应用程序页面的链接,例如:

加载module1Index.html,并在该页面上放置一个指向module2Index.html:的链接

<a href="http://module2.com"></a>

如果你需要两个模块放在同一页上,只需在另一页中包含一个:

angular.module('module1', ['module2']);