AngularJS 在同一个界面启动多个ng-app应用模块详解
#代码知识 发布时间: 2026-01-11
AngularJS默认在一个html界面中只启动一个

ng-app 模块,而且是界面中第一次出现的那个使用 ng-app 声明的模块,该问题可以通过查看AngularJS源代码或者是文档验证。
解决方案:
直接上代码,如果有兴趣了解其中缘由,可以选择阅读后面的部分;
<!DOCTYPE html>
<html>
<head lang="zh_CN">
<meta charset="UTF-8">
<title>AngularJS Source Code Analysis</title>
<script src="source/angular.min.js" type="text/javascript"></script>
<script src="source/angular-route.min.js" type="text/javascript"></script>
</head>
<body>
<div ng-app="myApp-0" ng-controller="nameCtrl">
<input type="text" ng-model="age"/>{{ demo }}--{{ age }}
<ul>
<li ng-repeat="val in names" ng-bind="val"></li>
</ul>
</div>
<!-- 并行启动多个ng-app -->
<div id="test-0" ng-controller="testCtrl_0">
<p>{{content.message}}</p>
</div>
<div id="test-1" ng-controller="testCtrl_1">
<p>{{content.message}}</p>
</div>
</body>
<script>
var myApp_0 = angular.module("myApp-0", []);
myApp_0.controller('nameCtrl', function ($scope, $rootScope){
$scope.names = ["shen", "amy", "sereno"];
$scope.age = 24;
$rootScope.demo = "demo";
});
var myApp_1 = angular.module("myApp-1", []);
myApp_1.controller('nameCtrl-1', function ($scope, $rootScope){
$scope.names = ["shen-1", "amy-1", "sereno-1"];
$rootScope.age = 24;
});
// 并行启动多个 ng-app
var myApp1mod = angular.module('test-0',[]);
myApp1mod.controller('testCtrl_0',function($scope){
var content= {};
content.message = "Hello Test-0";
$scope.content= content;
});
var myApp2mod = angular.module('test-1',[]);
myApp2mod.controller('testCtrl_1',function($scope){
var content= {};
content.message = "Hello Test-1";
$scope.content= content;
});
angular.element(document).ready(
function (){
angular.bootstrap(document.getElementById("test-0"), ["test-0"]);
angular.bootstrap(document.getElementById("test-1"), ["test-1"]);
}
);
</script>
</html>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
代码知识SEO上一篇 : JS基于面向对象实现的拖拽功能示例
下一篇 : Thrift的安装方法和简单实例
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!