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

πŸ”— AngularJS Component Communication & Interaction (2025 Guide)

🧲 Introduction – Why Learn Component Communication in AngularJS?

In modern single-page applications (SPAs), components rarely work in isolation. For an app to function cohesively, components must communicate with each other. In AngularJS 1.5+, components follow a structured and predictable communication model using bindings and event callbacks.

Whether you’re building a dashboard, form wizard, or product catalog, understanding how to implement parent-child interaction, data flow, and event propagation is essential to building scalable and modular AngularJS applications.

🎯 In this guide, you’ll learn:

  • How components interact in AngularJS
  • One-way and two-way data binding techniques
  • Passing functions from parent to child
  • Sibling and child-to-parent communication
  • Practical examples with line-by-line explanations

πŸ”„ Component Communication Types in AngularJS

πŸ“‘ Communication TypeπŸ” Description
Parent ➑️ ChildPass data using bindings (@, <)
Child ➑️ ParentTrigger callbacks using & bindings
Two-Way Binding (=)Share and update a common object between parent and child (via scope or service)
Sibling CommunicationUse a shared service or broadcast via $rootScope

🧩 1. Parent-to-Child Communication (@ and < Bindings)

The parent component passes data to the child using bindings.

βœ… Example

Parent Component:

app.component('parentComp', {
  template: `
    <child-comp username="'Vaibhav'" age="30"></child-comp>
  `
});

Child Component:

app.component('childComp', {
  bindings: {
    username: '@',
    age: '<'
  },
  controller: function() {
    this.$onInit = function() {
      console.log("Username:", this.username);
      console.log("Age:", this.age);
    };
  },
  template: `
    <p>Name: {{$ctrl.username}}</p>
    <p>Age: {{$ctrl.age}}</p>
  `
});

🧾 @ binds strings, while < allows dynamic value binding.


πŸ” 2. Child-to-Parent Communication (& Binding)

Child components can invoke a method defined in the parent using the & binding.

βœ… Example

Parent Component:

app.component('parentComp', {
  controller: function() {
    this.showAlert = function(message) {
      alert("Received from child: " + message);
    };
  },
  template: `
    <child-comp on-send="$ctrl.showAlert(msg)"></child-comp>
  `
});

Child Component:

app.component('childComp', {
  bindings: {
    onSend: '&'
  },
  controller: function() {
    this.sendMessage = function() {
      this.onSend({ msg: "Hello from child!" });
    };
  },
  template: `<button ng-click="$ctrl.sendMessage()">Send to Parent</button>`
});

🧾 & allows child to call parent’s method with arguments.


πŸ”„ 3. Two-Way Binding via Shared Object (= in Directives or < with Watchers)

AngularJS components prefer unidirectional data flow, but two-way interaction is possible using objects.

app.component('sharedCounter', {
  bindings: {
    counter: '<'
  },
  controller: function($scope) {
    $scope.$watch('$ctrl.counter.value', function(newVal) {
      console.log("Updated:", newVal);
    });
  },
  template: `
    <p>Value: {{$ctrl.counter.value}}</p>
    <button ng-click="$ctrl.counter.value = $ctrl.counter.value + 1">Increment</button>
  `
});

Parent Template:

<shared-counter counter="$ctrl.sharedObj"></shared-counter>

🧾 Use objects to mimic two-way binding between components.


πŸ”— 4. Sibling Component Communication via Shared Service

βœ… Step 1: Define Shared Service

app.service('MessageBus', function() {
  this.message = "";
});

βœ… Sibling A – Sender

app.component('siblingA', {
  controller: function(MessageBus) {
    this.send = function() {
      MessageBus.message = "Hello from Sibling A";
    };
  },
  template: `<button ng-click="$ctrl.send()">Send Message</button>`
});

βœ… Sibling B – Receiver

app.component('siblingB', {
  controller: function(MessageBus, $scope) {
    $scope.$watch(() => MessageBus.message, (newVal) => {
      this.received = newVal;
    });
  },
  template: `<p>Received: {{$ctrl.received}}</p>`
});

🧾 Services act as a central data hub for cross-component interaction.


πŸ”€ Advanced Pattern – Parent Invoking Child Methods

You can access child component controllers using require.

app.component('parentComp', {
  template: `
    <child-comp id="childRef"></child-comp>
    <button ng-click="$ctrl.callChild()">Call Child</button>
  `,
  controller: function($element) {
    this.callChild = function() {
      var childCtrl = angular.element(document.getElementById('childRef')).controller('childComp');
      childCtrl.sayHello();
    };
  }
});
app.component('childComp', {
  controller: function() {
    this.sayHello = function() {
      alert("Hello from Child Component");
    };
  },
  template: `<p>I am the Child Component</p>`
});

πŸ“Œ Summary – Recap & Next Steps

AngularJS component communication is essential for building connected, modular apps. Using input/output bindings and shared services, components can interact predictably, making large-scale architecture more maintainable and testable.

πŸ” Key Takeaways:

  • Use @ for string input, < for data binding, and & for callbacks
  • Use shared objects or services for complex two-way or sibling communication
  • Follow unidirectional data flow where possible for clarity and debugging ease

βš™οΈ Real-world Relevance:
Enterprise AngularJS apps use these patterns for dashboard widgets, admin panels, user forms, and real-time data displays.


❓ FAQ – AngularJS Component Communication


❓ How does a child component pass data to its parent in AngularJS?
βœ… Use & bindings to call parent methods from the child and pass data as parameters.


❓ Can two components share the same data in AngularJS?
βœ… Yes. Use a shared service or a shared object reference passed via bindings.


❓ What is the difference between @ and < in bindings?
βœ… @ passes a string literal, while < binds to a variable and updates dynamically.


❓ Is two-way binding still possible in AngularJS 1.5+ components?
βœ… Not directly with =, but it can be simulated using object references and watchers.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”— AngularJS Component Communication & Interaction

Or Copy Link

CONTENTS
Scroll to Top