1

I have two ng-app but it's not working simultaneously. If I'd comment-out, then the other section is working and vice versa.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <title>Angular</title>
</head>
<body>
    //first section
    <div id = "section1" ng-app="myapp" ng-controller="myctrl" ng-init="company='Example'; location = 'Earth'">
        <input ng-model="name">
        <h1 ng-bind="name"></h1>
        <h1>Welcome {{name}} to {{company}} at {{location}}</h1>
        <p>{{site}} {{setting}}</p> //running from controller myapp
    </div>
    //second section
    <div id = "section2" ng-app="myapp2" ng-controller="myctrl2"> {{x}} {{y}} </div>
    <script>
        var app = angular.module("myapp",[]);
        app.controller("myctrl", function($scope){
            $scope.site  = "www.example.com";
            $scope.setting = "Bliktzgreig";
        });
        var app2 = angular.module("myapp2",[]);
        app2.controller("myctrl2", function($scope){
            $scope.x = "Afforestation";
            $scope.y = "Deforestation";
        });
    </script>
</body>
</html>

Nature
Welcome Nature to Example at Earth www.example.com Bliktzgreig Afforestation Deforestation

SajZ
  • 262
  • 3
  • 16
  • Possible duplicate of [AngularJS Multiple ng-app within a page](https://stackoverflow.com/questions/18571301/angularjs-multiple-ng-app-within-a-page) – snak Aug 09 '19 at 05:49

1 Answers1

0

The ng-app directive is used to auto-bootstrap an AngularJS application. And according to AngularJS Documentation, only one AngularJS application can be auto-bootstrapped per HTML document. I'd like to quote a section from the documentation here:

Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead. AngularJS applications cannot be nested within each other.

You can try some alternatives for bootstrap second app http://shrestha-manoj.blogspot.com/2014/06/can-angularjs-have-multiple-ng-app.html

Maksym Petrenko
  • 1,053
  • 1
  • 6
  • 15