当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > AngularJS 在同一个界面启动多个ng-app应用模块详解

AngularJS 在同一个界面启动多个ng-app应用模块详解

2018年05月03日  | 移动技术网IT编程  | 我要评论

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>



感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网