π AngularJS Nested Components (2025 Guide)
π§² Introduction β Why Learn Nested Components in AngularJS?
As applications grow, splitting functionality into smaller, reusable components becomes essential. AngularJS (1.5+) supports nested components, allowing developers to build hierarchical and maintainable UI structures.
Nested components enable parent-child communication, data sharing via bindings, and encapsulation of UI logic, which is crucial for developing real-world applications like dashboards, forms, and lists.
π― In this guide, youβll learn:
- What nested components are in AngularJS
- How to pass data between parent and child components
- How to structure and render nested views
- Real-world examples with clear explanations
π§© What Are Nested Components?
Nested components refer to placing one AngularJS component inside another. This allows components to communicate and form a tree-like structure.
<parent-comp>
<child-comp></child-comp>
</parent-comp>
The parent passes data to the child, and optionally listens to events from the child.
π οΈ How to Create Nested Components
Letβs walk through a simple example of parent-child components.
β Step 1: Define the Parent Component
app.component('parentComp', {
template: `
<div>
<h2>Parent Component</h2>
<p>Parent Name: {{$ctrl.parentName}}</p>
<child-comp child-name="$ctrl.parentName"></child-comp>
</div>
`,
controller: function() {
this.parentName = 'AngularJS';
}
});
β Step 2: Define the Child Component
app.component('childComp', {
bindings: {
childName: '@'
},
template: `
<div style="margin-left: 20px;">
<h4>Child Component</h4>
<p>Received from Parent: {{$ctrl.childName}}</p>
</div>
`,
controller: function() {}
});
β Step 3: Render the Parent in HTML
<parent-comp></parent-comp>
π§Ύ Explanation:
parentComp
sets the valueparentName
as"AngularJS"
.- It passes the value to
childComp
viachild-name="$ctrl.parentName"
. childComp
receives it usingbindings: { childName: '@' }
.
π One-Way Binding with <
To use dynamic bindings, use <
instead of @
.
Updated Child Component:
app.component('childComp', {
bindings: {
childName: '<'
},
template: `<p>Live Parent Name: {{$ctrl.childName}}</p>`,
controller: function() {}
});
Now if the parent value changes, the child updates automatically.
β‘ Passing Functions from Parent to Child (&
Binding)
Child can trigger a method defined in the parent.
Parent Component:
app.component('parentComp', {
template: `
<child-comp on-log="$ctrl.sayHello()"></child-comp>
`,
controller: function() {
this.sayHello = function() {
alert("Hello from Parent!");
};
}
});
Child Component:
app.component('childComp', {
bindings: {
onLog: '&'
},
template: `<button ng-click="$ctrl.onLog()">Call Parent</button>`,
controller: function() {}
});
π§ͺ Real-World Example β Todo App with Nested Components
Letβs break the app into a todoList
(parent) and todoItem
(child) component.
β Parent: todoList Component
app.component('todoList', {
template: `
<div>
<h2>My Todos</h2>
<input ng-model="$ctrl.newTask" placeholder="New Task">
<button ng-click="$ctrl.addTask()">Add</button>
<todo-item ng-repeat="task in $ctrl.tasks" task="task"></todo-item>
</div>
`,
controller: function() {
this.tasks = ['AngularJS'];
this.newTask = '';
this.addTask = function() {
if (this.newTask) {
this.tasks.push(this.newTask);
this.newTask = '';
}
};
}
});
β Child: todoItem Component
app.component('todoItem', {
bindings: {
task: '<'
},
template: `<li>{{ $ctrl.task }}</li>`,
controller: function() {}
});
β HTML Entry Point:
<todo-list></todo-list>
π Use Cases for Nested Components
β
Dashboard widgets
β
User profile with child info cards
β
Product catalog with item detail views
β
Form wizard steps
β
Modular UI layout components
π Summary β Recap & Next Steps
AngularJS nested components are essential for building modular and scalable UIs. By structuring apps into parent-child components with data bindings, developers can isolate logic, encourage reusability, and manage state effectively.
π Key Takeaways:
- Use
@
for static string binding and<
for dynamic data - Use
&
to pass parent functions to child - Nesting improves modularity and simplifies large UIs
βοΈ Real-world Relevance:
Nested components are widely used in enterprise AngularJS apps where large interfaces require structured and encapsulated design.
β FAQ β AngularJS Nested Components
β What are AngularJS nested components?
β
They are components placed inside other components to create parent-child relationships and organize UI hierarchically.
β How do I pass data from a parent to a child component?
β
Use bindings: { property: '@' }
for strings or bindings: { property: '<' }
for dynamic values.
β How does a child component call a parent function?
β
Pass a callback using bindings: { action: '&' }
and invoke it with $ctrl.action()
.
β Can components be deeply nested in AngularJS?
β
Yes. You can nest components at multiple levels to match your UI and logic structure.
Share Now :