angularJS:一个页面多个ng-app

Posted on 2020-11-13 10:07  像马老师一样温文尔雅  阅读(97)  评论(0)    收藏  举报
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $rootScope) {
    $scope.names = ["Emil", "Tobias", "Linus"];
    $rootScope.lastname = "Refsnes";
});
    
var app2 = angular.module('myApp2111', []);    
app2.controller('myCtrl2', function($scope,$rootScope) {
    $scope.names = ["Emil", "Tobias", "Linus"];
    $rootScope.lastname = "Refsnes";
});
angular.bootstrap(document.getElementById("myApp2111"), ['myApp2111']);
//angular.bootstrap(document.getElementById(ID), [ID]);
//angular.bootstrap($("dir"),['dir']);
//这是两个,多个的话继续加一行 angular.bootsara

也可以:在第二个ng-app接管的div上要加一个id属性dir并且去掉第二个ng-app

<!Doctype html>
<html>
    <head>
    <meta charset="utf-8">
        <title></title>
        <style> </style> 
        <script src="js/jquery.min.js"></script>
        <script src ="js/angular.js"></script>
    </head>
    <body >
    <div ng-app="myapp">

        <hello></hello>
    </div>
    <div id="dir" >
        <dir></dir>
    </div>
        <script >       
        var app = angular.module("myapp",[]);
        app.directive("hello",function(){
            return {
                restrict:"E",
                template:'<h1>Hello angular</h1>',
                replace:true
            }
        });
        var app2 = angular.module("dir",[]).run(function($templateCache){
            $templateCache.put("header.html","<h2>use templateCache</h2>");
        });
        app2.directive("dir",function($templateCache){return {
            restrict:"E",
            template:$templateCache.get("header.html"),
            replace:true
        }});
        angular.bootstrap($("dir"),['dir']);
    </script>
</body>
</html>

 

转载自:https://blog.csdn.net/u013830811/article/details/50571639?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.channel_param

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3