Categories: JavascriptUI

AngularJS – Why & How to Create Custom Module

This article represents key concepts and code samples on why and how to create a custom module with AngularJS. 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:
  • Why create a custom module?
  • How to create a custom module?

 

Why Create A Custom Module?

A module in AngularJS wires together some of the following components:

  • Controllers
  • Directives
  • Filters
  • Service

 

In simple words, a module could also be thought of as container of above components. As per the design, if there is a need to bundle together one or more of the above and reuse it in one or more modules in future, one could go for creating a module. From the design perspective, following could be considered as best practice:
  • A module per feature could be designed.
  • It would be good to group similar/related service, filters and directives as part of the module for future reuse.

 

How to Create A Custom Module?

Creating a custom module is as simple as using angular.module API thereby passing the name and module dependencies and, defining one or more components. Take a look at following:

angular.module( "ModuleName", [] )
.factory( "ServiceName", [ 'depName', function( depName){
 // code goes here...
}])
.filter( "filterName", [ 'depName', function(depName)]{
 // code goes here...
})
.directive( "directiveName", [ 'depName', function( depName ){
 // code goes here...
}])

At lower level, a module is primarily a collection of two kind of blocks that are following:

  • Configuration blocks: Configuration blocks get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. The above code, actually, is applied as configuration blocks in that order in the following way:
    angular.module('ModuleName', []).
      config(function($provide, $compileProvider, $filterProvider) {
        $provide.factory('ServiceName', [ 'depName', function( depName){
     // code goes here...
     }]);
        $filterProvider.register('filterName', [ 'depName', function(depName)]{
     // code goes here...
     }]);
        $compileProvider.directive('directiveName', [ 'depName', function( depName ){
     // code goes here...
     }])
      });
  • Run blocks: Run blocks get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks.

 

[adsenseyu1]

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

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

7 days ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

2 weeks ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

2 weeks ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

3 weeks ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

3 weeks ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

3 weeks ago