下面一段代碼給大家介紹了Angular在一個頁面中使用兩個ng-app的方法,具體代碼如下所示:
<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8"> <title>ng-app指令,angular找到第一個ng-app就不會再找了,在一個頁面中只使用一個ng-app.</title> </head> <body> <input type="button" ng-app="myApp1" ng-controller="App1Controller" ng-click="do1()" value="按鈕1" /> <input type="button" ng-app="myApp2" ng-controller="App2Controller" ng-click="do2()" value="按鈕2"> <script src="bower_components/angular/angular.js"></script> <script> /** * myApp1 Module * * Description */ var myApp1 = angular.module('myApp1', []); myApp1.controller('App1Controller', ['$scope', function($scope){ $scope.do1 = function(){ console.log(11111); }; }]); var myApp2 = angular.module('myApp2', []); myApp2.controller('App2Controller', ['$scope', function($scope){ $scope.do2 = function(){ console.log(22222); }; }]); //手動的讓第二個div被myapp2管理 angular.bootstrap(document.querySelector('[ng-app="myApp2"]'), ['myApp2']); </script> </body></html>這種方法不推薦,請參考下篇文章。下面給大家介紹angular在一個頁面中使用兩個ng-app的方法(二)。敬請關注!
新聞熱點
疑難解答