Angular uses two ng app methods in one page

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

The following code shows you how Angular uses two ng-app in one page. 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>
  <input type="button" ng-app="myApp1" ng-controller="App1Controller" ng-click="do1()" value=" Button 1" />
  <input type="button" ng-app="myApp2" ng-controller="App2Controller" ng-click="do2()" value=" Button 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);
   };
  }]);
  // Manual yield 2 A div Be myapp2 Management 
  angular.bootstrap(document.querySelector('[ng-app="myApp2"]'), ['myApp2']);
  </script>
 </body>
</html>

This method is not recommended, please refer to the next article. Here's how angular uses two ng-app in one page (2). Please pay attention!


Related articles: