Categories: Javascript

AngularJS – Non-nested Directive-to-Directive Communication – Code Example

This article presents concepts and code example around non-nested directives-to-directives communication in AngularJS. The demo for the code example presented later in this article can be accessed on this page, Demo – Non-nested Directives-to-Directives Communication. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

 

Following are the key points described later in this article:
  • What is Directive-to-Directive Communication?
  • Service Helps Directives Communicate
  • Code Example – Non-nested Directives Communication

 

What is Directive-to-Directive Communication?

When the change in one or more attributes of one directive need to trigger one or more events in other directives resulting in updating of their attributes and related view, this can be termed as directive-to-directive communication. I, recently, open-sourced an Angular QuizApp which demonstrates the non-nested directive-to-directive communication. In the QuizApp, there are two directives, namely, iquestion and iscorecard. As one answers a question (iquestion directive), the score gets updated and displayed as part of iscorecard directive. Take a look at live example on following page: ATG Platform – Test 1

 

Service Helps Directives Communicate

As the directives are non-nested, meaning not placed within each other, it presents challenge on how to have them communicate with each other. With nested directives, controllers within directives define APIs that are used to communicate with each other. With non-nested directives, one can have them communicate using following method:

  • Define an object that consists of attributes whose change can trigger one or more event.
  • Define an Angular service that returns the above object using “factory” recipe method.
  • Inject the service into directives which needs to communicate.
  • Within directives, watch for change in the service attributes using scope.$watch API and take appropriate action.

 

Code Example – Non-nested Directives Communication

Pay attention to some of the following in the code example below:

  • helloservice defined using “factory” recipe method
  • helloservice injected into hello and bye directives
  • scope.$watch API used to watch change in name attribute of the service and assign the value to name attribute
angular.module('HelloModule', [])
.directive( 'hello', function( helloservice) {
 return {
  restrict: "E",
  scope:{
   name: '@'
  },
  controller: function( $scope, helloservice ) {
   $scope.updateName = function() {
     helloservice.setName( $scope.name );
   };
  },
  link: function( scope, element, attrs ) {
    helloservice.setName( scope.name );
  },
  templateUrl: '/assets/templates/hello1.html'
 }
})
.directive( 'bye', function( helloservice ) {
 return {
  restrict: "E",
  scope:{   
  },
  controller: function( $scope, helloservice ) {
    $scope.name = helloservice.getName();  
  },
  link: function( scope, element, attrs ) {
    scope.$watch(function() {
    return helloservice.name;
   }, function() {
    scope.name = helloservice.name;
   });
  },
  templateUrl: '/assets/templates/bye.html'
 }
})
.factory('helloservice', function(){
 return new User();
});

function User() {
 this.name = '';

 this.setName = function( name ) {
  this.name = name;
 };

 this.getName = function() {
  return this.name;
 };
}

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar
Tags: angularjs

Recent Posts

Coefficient of Variation in Regression Modelling: Example

When building a regression model or performing regression analysis to predict a target variable, understanding…

1 day ago

Chunking Strategies for RAG with Examples

If you've built a "Naive" RAG pipeline, you've probably hit a wall. You've indexed your…

1 week ago

RAG Pipeline: 6 Steps for Creating Naive RAG App

If you're starting with large language models, you must have heard of RAG (Retrieval-Augmented Generation).…

1 week ago

Python: List Comprehension Explained with Examples

If you've spent any time with Python, you've likely heard the term "Pythonic." It refers…

2 weeks ago

Large Language Models (LLMs): Four Critical Modeling Stages

Large language models (LLMs) have fundamentally transformed our digital landscape, powering everything from chatbots and…

3 months ago

Agentic Workflow Design Patterns Explained with Examples

As Large Language Models (LLMs) evolve into autonomous agents, understanding agentic workflow design patterns has…

3 months ago