Angularjs啟動應用
阿新 • • 發佈:2018-09-22
asc bsp 自動 單獨 應用 全部 數組 ifg 兩個 要啟動一個angular應用,可以使用ng-app指令,也可以調用bootstrap方法手動啟動。
一個angular應用中,可以有多個angular.module。應該有且只有一個angular.module的名稱與ng-app的值一致,否則就沒有意義了。
angular.module(‘M1‘,[]); angular.module(‘M2‘,[]); ...... angular.module(‘Mn‘,[]); angular.module(‘app‘,[‘M1‘,‘M2‘,...,‘Mn‘]);
M1,M2,...,Mn可能是不同的業務模塊,可以單獨作為一個angular.module,最後全部掛載在模塊下。
以上是自動加載。如果采用手動加載,則不受名稱限制,不受數量限制。
angular.bootstrap(element, [modules], [config]); ● element(必需)。要啟動angular的根節點,一般為document,也可以是其他的的html的dom。 ● modules(數組,可選)。依賴的模塊。 ● conifg(object,可選)。配置項,目前只有strictDi一個可配置項,默認為false,是否開啟輔助debug。
<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <body> <div id="app1"> <div ng-controller="myCtrl"> {{ hello }} </div> </div> <div id="app2"> <div ng-controller="myCtrl"> {{ hello }} </div> </div> <script type="text/javascript"> // AngularJS 模塊定義應用: var app1 = angular.module("test1",[]); // AngularJS 控制器控制應用: app1.controller("myCtrl",fu nction($scope){ $scope.hello = "a Angular app"; }); var app2 = angular.module("test2",[]); app2.controller("myCtrl",function($scope){ $scope.hello = " another Angular app"; }); angular.bootstrap(document.getElementById("app1"),[‘test1‘]); angular.bootstrap(document.getElementById("app2"),[‘test2‘]); </script> </body> </html>
以上例子,啟動了兩個angular app,且沒有使用指令
Angularjs啟動應用