Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple controller in single page in AngularJS

I am new to AngularJS I have a problem with this code. I want to add multiple controller in single ng-app. But it execute first one. Why not second one?

<!DOCTYPE html>
<html ng-app="myapp">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angul /1.4.8/angular.min.js"></script>
</head>
<body>
    <div ng-controller="cont1">
        <input type="text" ng-model="fullname">
        {{fullname}}
    </div>
    <div ng-controller="cont2">
        <input type="text" ng-model="fname">
        {{fname}}
    </div>
    <script>
        var app = angular.module("myapp", []);
        app.controller('cont1', function ($scope) {
            $scope.fullname = "";
        });
        var new = angular.module('myapp', []);
        new.controller('cont2', function ($scope) {
            $scope.fname = "";
        });
    </script>
</body>
</html>
like image 959
RohanArihant Avatar asked Jan 29 '16 06:01

RohanArihant


People also ask

Can we have multiple controllers in AngularJS?

An AngularJS application can contain one or more controllers as needed, in real application a good approach is to create a new controller for every significant view within the application.

Can multiple controllers be used in a single HTML page?

Multiple controllers can be used in a single html page, provided they all belong to the same module.

Can we have multiple Ng app in single page?

Only one ngApp directive can be auto-bootloaded per HTML Document but you can have multiple apps as long as you manually bootstrap the subsequent ones.

Can we have multiple modules in AngularJS?

Yes, you can define multiple modules in angularJS as given below. The modularization in AngularJS helps us to keep the code clarity and easy to understand, as we can combine multiple modules to generate the application.


2 Answers

Because you are overwriting the first myapp module when you do var new= angular.module('myapp',[]);.

Your code should be:

var app = angular.module("myapp", []);
app.controller('cont1', function($scope) {
  $scope.fullname = "";
});
app.controller('cont2', function($scope) {
  $scope.fname = "";
});

or

var app = angular.module("myapp", []);
app.controller('cont1', function($scope) {
  $scope.fullname = "";
});
angular.module("myapp").controller('cont2', function($scope) {
  $scope.fname = "";
});

The second parameter[] passed to module() makes the difference

like image 169
T J Avatar answered Sep 23 '22 14:09

T J


To best way to define controllers, directives, factories etc... is

define your modules names in a separate file

app.module.js

angular.module("myapp",[]); // inside [] you define your module dependencies

for controllers create separate file (depending on your requirement even you can create 1 file for 1 controller)

some.controller.js

angular.module("myapp").controller('someCtrl'['$scope', function($scope){

}]);

angular.module("myapp").controller('someOtherCtrl'['$scope', function($scope){

}]);

NOTE:

Two types you can write controller

TYPE1 (not recomended)

.controller('ctrlName', function($scope){

});

TYPE2 (recomended)

.controller('ctrlName', ['$scope', function($scope){

}]);

Reason

So as you can see in the TYPE2 we are passing controller dependencies in an array, so when we compile our program angular will give the name as eg:a to $scope inside function() and treat it as $scope.

With the TYPE1 you need to follow specific order while defining controller dependency otherwise angular will through error because in this approach angular simply treats first dependency as $rootscope, second as $scope and so on....

For Eg:

you can't pass dependencies to your controller like this

.controller('ctrlName', function($http, $scope) {

});

this will throw error

if you define like

.controller('ctrlName', function($scope, $http) {

    });

this will work fine since its in order that angular wants.

like image 36
Pratap A.K Avatar answered Sep 24 '22 14:09

Pratap A.K