AngularJS 在同一個界面啟動多個ng-app應(yīng)用模塊詳解
更新時間:2016年12月20日 09:45:23 投稿:lqh
這篇文章主要介紹了AngularJS 在同一個界面啟動多個ng-app應(yīng)用模塊詳解的相關(guān)資料,需要的朋友可以參考下
AngularJS默認(rèn)在一個html界面中只啟動一個
ng-app 模塊,而且是界面中第一次出現(xiàn)的那個使用 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>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- AngularJS使用ng-app自動加載bootstrap框架問題分析
- AngularJS框架的ng-app指令與自動加載實現(xiàn)方法分析
- AngularJS ng-app 指令實例詳解
- 基于AngularJS實現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能
- AngularJS入門示例之Hello World詳解
- Angularjs通過指令監(jiān)聽ng-repeat渲染完成后執(zhí)行腳本的方法
- angularjs中ng-attr的用法詳解
- Angularjs中ng-repeat-start與ng-repeat-end的用法實例介紹
- AngularJS實現(xiàn)網(wǎng)站換膚實例
- AngularJS使用帶屬性值的ng-app指令實現(xiàn)自定義模塊自動加載的方法
相關(guān)文章
Angular2學(xué)習(xí)教程之組件中的DOM操作詳解
這篇文章主要給大家介紹了Angular2學(xué)習(xí)教程之組件中DOM操作的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來看看吧。2017-05-05詳解什么是@ngrx/store開發(fā)包中的MemoizedSelector
這篇文章主要為大家介紹了@ngrx/store開發(fā)包中的MemoizedSelector使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Angular6實現(xiàn)拖拽功能指令drag實例詳解
這篇文章主要為大家介紹了Angular6實現(xiàn)拖拽功能指令drag實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11AngularJs學(xué)習(xí)第八篇 過濾器filter創(chuàng)建
這篇文章主要介紹了AngularJs學(xué)習(xí)第八篇 過濾器filter創(chuàng)建的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06