0

I'm currently learning Angularjs and am trying to work out why the expression in the 2nd <div> isn't working. It's displaying:

{{ firstName1 + " " + lastName1 }}

instead of:

John1 Doe1

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

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

<div ng-app="myApp1" ng-controller="myCtrl1">
{{ firstName1 + " " + lastName1 }}
</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.firstName1 = "John1";
    $scope.lastName1 = "Doe1";
});

</script>

</body>
</html>

EDIT:

To get this to work, do I need to remove ng-app="myApp1" ? What arguments do I pass in to angular.bootstrap() to get this to work?

David Klempfner
  • 8,700
  • 20
  • 73
  • 153

1 Answers1

1

There are a few things to keep in mind when using ngApp:

  • 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.

See AngularJS ng-app Directive API Reference

georgeawg
  • 48,608
  • 13
  • 72
  • 95