Recently learning AngularJS, when writing a small Demo program encountered a problem, that is, when multiple Ng-app are loaded on a single page, only the first one will work.
Find the answer after the query: on the angular page, only one ng-app per HTML page will be loaded automatically.
So we have several solutions to solve this problem.
1. It is easy to merge multiple Ng-app into a single root module.
2. You can manually load multiple Ng-app except the first one.
Ng-app is a very important label in the angular. A ng-app should correspond to a piece of business logic, so a page might need to use multiple modules. To avoid the coupling of code between different modules, I personally prefer the second approach. The code is as follows:
<DivID= "App1div"Ng-app= "MyApp1"Ng-controller= "Personctrl"> <P>Personctrl</P>First name:<inputtype= "text"Ng-model= "FirstName"><BR>Last Name:<inputtype= "text"Ng-model= "LastName"><BR>Full Name: {{fullName ()}}</Div> <BR><BR> <DivID= "App2div"Ng-app= "MyApp2"Ng-controller= "Personctrl"> <P>Personctrl</P>First name:<inputtype= "text"Ng-model= "FirstName"><BR>Last Name:<inputtype= "text"Ng-model= "LastName"><BR>Full Name: {{fullName ()}}</Div> <Script>var App1 = angular.module (' MyApp1 ', []); var app2 = angular.module (' myApp2 ', []); App1.controller (' Personctrl ', function ($scope) {$scope. firstName = ' Aaron '; $scope. LastName = ' S '; $scope. FullName = function () {return $scope. firstName + $scope. LastName;} }); App2.controller (' Personctrl ', function ($scope) {$scope. firstName = ' Andy '; $scope. LastName = ' W '; $scope. FullName = function () {return $scope. firstName + $scope. LastName;} }); Angular.bootstrap (document.getElementById ("App2div"), [' myApp2 ']); </Script>
Issues with starting multiple Ng-app in AngularJS