0

How to use 2 angularjs apps in same html page?

Here is the code to reproduce, where 2nd app fails:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<div ng-app="myApp1" ng-controller="myCtrl1">
{{ firstName + " " + lastName }}
</div>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});

var app1 = angular.module("myApp1", []);
app1.controller("myCtrl1", function($scope) {
    $scope.firstName = "John1";
    $scope.lastName = "Doe1";
});

</script>

</body>
</html>

Also I have tried to use angular.bootstrap with no luck like suggested here

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<div ng-app="myApp1" ng-controller="myCtrl1">
{{ firstName + " " + lastName }}
</div>


<script>

var app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});

angular.bootstrap(document, ['myApp']);


var app1 = angular.module("myApp1", []);

app1.controller("myCtrl1", function($scope) {
    $scope.firstName = "John1";
    $scope.lastName = "Doe1";
});

angular.bootstrap(document, ['myApp1']);

</script>

</body>
</html>

Update:

Working example:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div id="firstApp" ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<div id="firstApp1" ng-app="myApp1" ng-controller="myCtrl1">
{{ firstName + " " + lastName }}
</div>


<script>

var app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});

let appEl1 = document.getElementById("firstApp");
angular.bootstrap(appEl1, ['myApp']);


var app1 = angular.module("myApp1", []);

app1.controller("myCtrl1", function($scope) {
    $scope.firstName = "John1";
    $scope.lastName = "Doe1";
});

let appEl2 = document.getElementById("firstApp1");
angular.bootstrap(appEl2, ['myApp1']);

</script>

</body>
</html>

2
  • 1
    Possible duplicate of AngularJS Multiple ng-app within a page Commented Jan 25, 2018 at 15:56
  • Inject one app inside other app.. in other words, make one root app. will work. Commented Jan 25, 2018 at 15:57

2 Answers 2

1

The problem is you are bootstrapping both apps on document. It needs to be on the element(div in your case) that you want it to be according to angular.bootstrap documentation here

Eg:

<div id="firstApp">

let appEl1 = document.getElementById("firstApp");
angular.bootstrap(appEl1, ['myFirstApp']);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use

angular.bootstrap(document.getElementById("appID"), ['myApp1']);

with

<div id="appID" ng-app="myApp1" ng-controller="myCtrl1">

This will bootstrap it manually.

(You also had a typo(?) in your first case with registering a controller: app.controller("myCtrl1" should be app1.controller("myCtrl1")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.