2

I'm new to AngJS and I'm trying to write code on my own. I got stuck in one part.

Code:

<!DOCTYPE HTML>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<title> First page </title>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
    First Name: <input type="text" ng-model="person.firstName"> <br>
    Last Name: <input type="text" ng-model="person.lastName"> <br>
    <p> {{person.firstName + " " + person.lastName}} age is: {{age[0]}}</p>
</div>

<div ng-app="mApp" ng-controller="mCtrl">
    Registration number: <input type="number" ng-model="person.regNum"> <br>
    Class number: <input type="text" ng-model="person.classNum"> <br>
    <p> {{person.regNum, person.classNum}} </p>
</div>

<script>
var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope) {
    $scope.person = {firstName:"Nikhil",lastName:"Hegde"};
    $scope.age = [20,21,22];
    });

var app1 = angular.module('mApp',[]);
app1.controller('mCtrl',function($scope) {
    $scope.person = {regNum:"122503",classNum:"12EC48"};
    });

</script>
</body>
</html>

Output of the code

I'm not really sure what is wrong with the second pair of fields but the output as you can see is incorrect.

Also can I reuse the app variable? app = angular.module('myApp',[]);

Instead of using a separate 'app1' variable, can I also reuse the same variable 'app' as: app = angular.module('mApp',[]);

2
  • One more query along with the above; Is it possible to use one application and having two controllers for the same? Commented Sep 29, 2016 at 7:54
  • Yes you can have one ng-app with multiple controllers (see my answer) or you can have one ng-app with one controller (@Sajeetharan's answer) Commented Sep 29, 2016 at 7:57

3 Answers 3

2

You should have only one app

<!DOCTYPE HTML>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <title> First page </title>
</head>
<body ng-app="myApp">
    <div ng-controller="myCtrl">
        First Name: <input type="text" ng-model="person.firstName"> <br>
        Last Name: <input type="text" ng-model="person.lastName"> <br>
        <p> {{person.firstName + " " + person.lastName}} age is: {{age[0]}}</p>
    </div>

    <div ng-controller="mCtrl">
        Registration number: <input type="number" ng-model="person.regNum"> <br>
        Class number: <input type="text" ng-model="person.classNum"> <br>
        <p> {{person.regNum, person.classNum}} </p>
    </div>

<script>
    var app = angular.module('myApp',[])
    .controller('myCtrl',function($scope) {
        $scope.person = {firstName:"Nikhil",lastName:"Hegde"};
        $scope.age = [20,21,22];
    })
    .controller('mCtrl',function($scope) {
        $scope.person = {regNum:"122503",classNum:"12EC48"};
    });

</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

I understood what you were trying to convey but I tried copy pasting your code to see if it works and it doesn't seem to. {{person.firstName + " " + person.lastName}} age is: {{age[0]}} {{person.regNum, person.classNum}} are the outputs of the two pairs of fields.
I must have made some kind of mistake when I ported your code. I used the concept of one module for an app and multiple controllers. It works! Thanks for your help! @Weedoze
2
 <body>
   <div ng-app="myApp">
     <div ng-controller="myCtrl">
       First Name: <input type="text" ng-model="person.firstName"> <br>
       Last Name: <input type="text" ng-model="person.lastName"> <br>
      <p> {{person.firstName + " " + person.lastName}} age is: {{age[0]}}</p>
    </div>

    <div ng-controller="mCtrl">
      Registration number: <input type="number" ng-model="person.regNum"> <br>
      Class number: <input type="text" ng-model="person.classNum"> <br>
     <p> {{person.regNum+ " "+ person.classNum}} </p>
  </div>
</div>

Script file

 <script>
   var app = angular.module('myApp',[]);
   app.controller('myCtrl',function($scope) {
     $scope.person = {'firstName':"Nikhil",'lastName':"Hegde"};
     $scope.age = [20,21,22];
   });

app.controller('mCtrl',function($scope) {
  $scope.person1 = {'regNum':"122503",'classNum':"12EC48"};
 });

define your app only once. You can add multiple controller to ng-module. Hope It will be useful.

1 Comment

I made some kind of a mistake when porting your code as well. Your concept works too! Thanks!
1

You can have only 1 module in an app, have different scope variable inside same controller

 var app = angular.module('myApp', []);
 app.controller("myCtrl", ["$scope", "$http",
   function($scope, $http) {
     $scope.person = {
       firstName: "Nikhil",
       lastName: "Hegde"
     };
     $scope.registration = {
       regNum: "122503",
       classNum: "12EC48"
     };
     $scope.age = [20, 21, 22];
   }
 ]);

DEMO

3 Comments

I removed the $http and the dependencies on the controller. I just made it app.controller("myCtrl", function(scope) { .... } It works but that's not the concept I'm trying to learn here. Thanks but!
It certainly helped but @Weedoze answer was the one I was looking for
@NikhilHegde its not about the answer, just a upvote

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.