Sivaprasad derangula
Sivaprasad derangula

Reputation: 1169

second controller is not executing in Angular JS?

I'm new to AngularJS. I wrote code like this,

var myApp2 = angular.module('myApp2', []);

myApp2.controller('studentController', function($scope) {
  $scope.student = {
    firstName: "first name",
    lastName: "last name",
    fullName: function() {
      var studentObject = $scope.student;
      return studentObject.firstName + " " + studentObject.lastName;
    }
  };
});

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

myApp1.controller('productController', function($scope) {
  $scope.quantity = 0;
  $scope.cost = 0;
  $scope.getTotalCost = function() {
    return $scope.quantity * $scope.cost;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="col-lg-3" ng-app="myApp1" ng-controller="productController">
  <label class="label label-default">Quantity :</label>
  <input type="number" class="form-control" ng-model="quantity" />
  <br>
  <br>
  <label class="label label-default">Cost :</label>
  <input type="number" class="form-control" ng-model="cost" />
  <br>
  <br>
  <label class="label label-success">Product :</label><span class="form-control disabled" ng-bind="getTotalCost()"></span>
</div>
<div class="col-lg-3" ng-app="myApp2" ng-controller="studentController">
  <label class="label label-default">First name:</label>
  <input type="text" class="form-control" ng-model="student.firstName" />
  <br>
  <br>
  <label class="label label-default">Last name:</label>
  <input type="text" class="form-control" ng-model="student.lastName" />
  <br>
  <br>
  <label class="label label-success">You are entering:</label> <span class="form-control disabled" ng-bind="student.fullName()"></span>
</div>

the second div is not initialized. I think the controller is not executing.

If I remove the first div then it does.

What's wrong with it?

Upvotes: 0

Views: 37

Answers (1)

Anupam Singh
Anupam Singh

Reputation: 1166

You will require manual bootstrapping for this, find link below :

https://stackoverflow.com/a/18583329/1939542

Upvotes: 1

Related Questions