How to Create Multiple Controllers in One Page in AngularJS

Muhammad Adil Feb 02, 2024
  1. Create Multiple Controllers in One Page in AngularJS
  2. Advantages of Using Multiple Controllers in One Page in AngularJS
How to Create Multiple Controllers in One Page in AngularJS

AngularJS is a JavaScript framework used for web applications. It is most famous for single-page applications and provides features like data binding, templating, MVC, dependency injection, etc.

AngularJS provides a way to organize the code into controllers and views. Controllers are responsible for input validation, data retrieval, and processing, while views are responsible for displaying data in UI components.

It allows the use of multiple controllers on a single-page app. This enhances the user experience by providing more straightforward navigation between different pages without reloading the page.

Multiple controllers make it easier for developers to understand the scope of each template and can improve performance by avoiding unnecessary DOM manipulations.

Moreover, multiple controllers are a good solution when you need more than one view on the same page or when you need different views on different parts of the page.

Create Multiple Controllers in One Page in AngularJS

To create multiple controllers, use ng-controller directives. It establishes a connection between the scope and the controller.

Let’s discuss a way to create two controllers. The first step is creating two controllers, one controller called Home and another called About. The Home controller will contain the home view, and the About controller will have the about view.

The second step is to create two views, one view called home and another view called about. We will place the home view in the Home controller and the about view in the About controller.

You can also use routing with the ngRoute module instead of ng-controller directives to achieve this functionality.

Example (JavaScript code):

var app = angular.module('myApp', []);
app.controller('HomeController', function ($scope) {
    $scope.controllerName = "HomeController";
});
app.controller('AboutController', function ($scope) {
    $scope.controllerName = "AboutController";
});

Example (HTML code):

<body ng-app="myApp">
    <h1>Example of Multiple Controllers</h1>
    <div ng-controller="HomeController"> Controller Name: {{controllerName}}
        <br>
        <input type="text" ng-model="message"/>
    </div>
    <br>
    <div ng-controller="AboutController"> Controller Name: {{controllerName}}
        <br> <br>
        <input type="text" ng-model="message"/>
    </div>
</body>

Click here to check the demonstration of the code above.

Advantages of Using Multiple Controllers in One Page in AngularJS

Using multiple controllers in AngularJS is beneficial for the following reasons:

  • It reduces the number of HTTP calls to the server.
  • It improves performance by caching the data.
  • It makes it easy to reuse code and components.
  • It provides a better separation of concerns.
  • It helps with debugging and testing.
Muhammad Adil avatar Muhammad Adil avatar

Muhammad Adil is a seasoned programmer and writer who has experience in various fields. He has been programming for over 5 years and have always loved the thrill of solving complex problems. He has skilled in PHP, Python, C++, Java, JavaScript, Ruby on Rails, AngularJS, ReactJS, HTML5 and CSS3. He enjoys putting his experience and knowledge into words.

Facebook

Related Article - Angular Controller