🧱 AngularJS Components & Lifecycle
Estimated reading: 4 minutes 280 views

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 value parentName as "AngularJS".
  • It passes the value to childComp via child-name="$ctrl.parentName".
  • childComp receives it using bindings: { 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 :
Share

πŸ” AngularJS Nested Components

Or Copy Link

CONTENTS
Scroll to Top