Angular uses two ng app methods of II in one page

  • 2021-07-22 08:49:20
  • OfStack

In the previous article, we introduced the method that Angular uses two ng-app in one page. This method is not recommended. Let's introduce Method 2 below. The specific code is as follows:


<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>ng-app Instruction ,angular Find the first 1 A ng-app You won't look for it again, in 1 Use only the 1 A ng-app.</title>
 </head>
 <body ng-app="myApp">
  <input type="button" ng-controller="App1Controller" ng-click="do1()" value=" Button 1" />
  <input type="button" ng-controller="App2Controller" ng-click="do2()" value=" Button 2">
  <script src="bower_components/angular/angular.js"></script>
  <script>
  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);
   };
  }]);
  /**
  * myApp Mod'myApp1','myApp2'ul;
  *
  * Description
  */
  angular.module('myApp', ['myApp1','myApp2']);
  </script>
 </body>
</html>

Is to wrap two small modules with one big module.


Related articles: