How to create nested controllers in Angular.js ?
Last Updated :
02 Feb, 2022
A controller in AngularJS is a JavaScript object created with the help of a JavaScript object constructor. A controller can contain properties and functions. Controllers are used for controlling the application data of an AngularJS application.
In this article, we will see the nested controllers in AngularJS and will understand their implementation with the help of examples.
The approach used to create a controller in AngularJS is to create a controller we first need to create an application module. A module is used to define an application.
Syntax:
var app=angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
...
});
Description:
- Once a module is created we add a controller to it using the controller() method.
- The first parameter inside the controller() method is the name of the controller.
- The second parameter is a function that has $scope as a parameter. The $scope object is injected into each controller by the AngularJS framework.
- Different properties and methods can be attached to the $scope object inside the controller function.
- To use a property of a controller or maintain the data in an HTML element, you need to specify the controller in the ng-controller directive.
Example 1: This example will illustrate how to create a controller
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 1</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<h1>{{title}}</h1>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope) {
$scope.title = "AngularJS Controller";
});
</script>
</body>
</html>
Output:
Example 2: This example will illustrate the attachment of different properties to the controller
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 2</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<h1>{{properties.name}}</h1>
<p>{{properties.subject}}</p>
<label>First name:</label>
<input type="text" ng-model="firstname">
<br><br>
<label>Last name:</label>
<input type="text" ng-model="lastname">
<br><br>
<p ng-show="firstname!=undefined && lastname!=undefined">
Hello {{displayname()}}
</p>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope) {
$scope.properties = {
"name": "GeeksforGeeks",
"subject": "AngularJS Controllers"
};
$scope.displayname = function () {
return $scope.firstname + " " + $scope.lastname;
};
});
</script>
</body>
</html>
Output:
Example 2
In the above example, we have added properties to the controller "myCtrl". A controller can have different properties like string, number, object, array, function.
Nested Controllers: AngularJS allows using nested controllers. It means that you have specified a controller in an HTML element which is a child of another HTML element using another controller. An important point to note here is that a child controller can access properties and methods belonging to a parent controller, but a parent controller cannot access the properties and methods of a child controller.Â
Syntax:Â
<div ng-controller="controller">
<div ng-controller="subcontroller">
</div>
</div>
Example 1: This example will illustrate how to create a nested controllers
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 3</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="parentController">
<div ng-controller="childController">
<p>{{title}}</p>
</div>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("parentController", function ($scope) {
$scope.title = "This is parent controller.";
});
app.controller("childController", function ($scope) {
});
</script>
</body>
</html>
Output:
Example 3
Here you can observe that the parent controller has a property called title which is inherited by the child controller.
Example 2: If we add the same property to both the parent and child controllers
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 4</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="parentController">
<div ng-controller="childController">
<p>{{title}}</p>
</div>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("parentController", function ($scope) {
$scope.title = "This is parent controller.";
});
app.controller("childController", function ($scope) {
$scope.title = "This is child controller.";
});
</script>
</body>
</html>
Output:
Example 4
Here you can observe that both the parent and child controller have the property named title. The property of the child controller overrides that of the parent controller.
Example 5: The below example shows how we can use nested controllers in AngularJS.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 5</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="parentController">
<div ng-controller="childController">
<div ng-controller="subtopicsController">
<h1>{{title}}</h1>
<p>Topics:</p>
<div>
<ul ng-repeat="x in topics">
<li>{{x}}</li>
</ul>
</div>
<div>
<p>Subtopics:</p>
<ul ng-repeat="y in subtopics">
<li>{{y}}</li>
</ul>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("parentController", function ($scope) {
$scope.title = "GeeksforGeeks";
});
app.controller("childController", function ($scope) {
$scope.topics = ["Expressions", "Modules", "Controllers"];
});
app.controller("subtopicsController", function ($scope) {
$scope.subtopics = [
"What are expressions?",
"How are they used in data binding?",
"What are modules in AngularJS?",
"What is their importance?",
"What are controllers?",
"What are nested controllers?"
];
});
</script>
</body>
</html>
Output:
Example 5
Similar Reads
How to create a new component in Angular? A component in Angular is the building block for making web pages. It is a reusable block of code that can be used anywhere in the app any number of times. It provides scalability, reusability, and readability. Each component does a specific job like showing a menu, a photo or a card, etc. In this a
3 min read
How to use filter within controllers in AngularJS ? In this article, we will see how to use the Filter inside the controller using AngularJS. Filters are used to format the value of an expression and display it to the user. It can be used in HTML Previews, Controllers or Services, and directives. AngularJS facilitates many built-in filters, although,
4 min read
How to share data between controllers in AngularJS ? The task is to share data variables between two or more controllers by using AngularJS. There are many procedures to achieve this. Here we will discuss the most popular ones. Approach: To share data between the controllers in AngularJS we have two main cases: Share data between parent and child: Her
3 min read
How to Create a new module in Angular ? Modules are most important when it comes to building strong and scalable Angular applications. They help organize your code, promote modularity, and improve maintainability. It encourages collaboration among developers by grouping related components, directives, pipes, and services. In this article,
3 min read
How to pass input to a custom directive in Angular JS? In AngularJS, passing input to a custom directive allows the customization of components within an application. Directives can receive input via attributes, expressions, or controller functions, allowing for dynamic behavior and data binding. In this article, we will explore two different approaches
3 min read