π 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 β‘οΈ Child | Pass data using bindings ( @,<) | 
| Child β‘οΈ Parent | Trigger callbacks using &bindings | 
| Two-Way Binding ( =) | Share and update a common object between parent and child (via scope or service) | 
| Sibling Communication | Use 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 :
