AngularJS 在同一個介面啟動多個ng-app應用模組詳解_AngularJS

來源:互聯網
上載者:User

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>

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.