π§° AngularJS NgModules & Configuration Patterns (2025 Guide)
π§² Introduction β Why Understand NgModules and Configuration Patterns?
In AngularJS, NgModules (or simply βmodulesβ) form the foundational building blocks for organizing applications. They help in grouping components, services, filters, directives, and routing logic into cohesive blocks of functionality. While AngularJS doesnβt use the @NgModule
decorator like Angular 2+, its module system supports structured configuration through .config()
, .run()
, and dependency declarations.
Mastering module configuration patterns allows developers to create scalable, maintainable, and testable AngularJS applicationsβespecially in enterprise-grade or legacy systems.
π― In this guide, youβll learn:
- What NgModules are in AngularJS
- How to declare and configure modules properly
- The purpose of
.config()
and.run()
blocks - Real-world examples of modular configurations
π§© What Is an NgModule in AngularJS?
In AngularJS, a module is a container for the various parts of your application: controllers, services, filters, directives, etc. It also defines application dependencies and can be configured at runtime.
var app = angular.module('myApp', []);
π§Ύ Explanation:
'myApp'
: Name of the module.[]
: Dependency list (empty for now).
Modules encourage a modular architecture where features and utilities can be split across different files and loaded as needed.
π¦ Declaring Multiple NgModules
You can create multiple modules for separation of concerns:
angular.module('userModule', []);
angular.module('productModule', []);
angular.module('sharedModule', []);
And inject them into the root module:
angular.module('myApp', ['userModule', 'productModule', 'sharedModule']);
βοΈ Using .config()
for Application Setup
The .config()
method is used to configure providers before the app runs. This is the right place for setting up:
- Routes
- Custom service providers
- Constants
- Interceptors
app.config(function($locationProvider, $httpProvider) {
$locationProvider.html5Mode(true);
$httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
});
π§Ύ Explanation:
$locationProvider
enables HTML5 routing.$httpProvider
customizes default HTTP headers.
π§ͺ Common .config()
Use Case β Routing Setup
app.config(function($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'home.html',
controller: 'HomeCtrl'
})
.otherwise({ redirectTo: '/home' });
});
π§Ύ Purpose: Defines view-controller mappings for SPA navigation.
π Using .run()
for Application Initialization
The .run()
block executes after the injector is created, useful for app-wide behavior.
app.run(function($rootScope) {
$rootScope.appTitle = "My AngularJS App";
});
π§Ύ Use Cases:
- Setting global variables
- Watching authentication status
- Logging/analytics initialization
π Full Example β Modular Configuration
π§± Step 1: Shared Module
angular.module('sharedModule', [])
.constant('APP_VERSION', '1.0.0');
π¦ Step 2: User Module
angular.module('userModule', [])
.controller('UserCtrl', function($scope) {
$scope.name = "John Doe";
});
π Step 3: Root Module
angular.module('myApp', ['sharedModule', 'userModule'])
.config(function($logProvider) {
$logProvider.debugEnabled(true);
})
.run(function($rootScope, APP_VERSION) {
$rootScope.version = APP_VERSION;
});
π Suggested Folder Structure for Modular AngularJS Apps
angularjs-app/
β
βββ index.html
βββ app/
β βββ app.module.js β Root module
β βββ app.config.js β .config() block
β βββ app.run.js β .run() block
β
βββ user/
β βββ user.module.js
β βββ user.controller.js
β
βββ shared/
β βββ shared.module.js
β βββ constants.js
β Benefits of Modular Configuration Patterns
βοΈ Better code organization β Easier to manage large codebases
βοΈ Lazy loading ready β Each feature can load independently
βοΈ Testability β Isolated modules and components are easier to test
βοΈ Scalability β Add or remove features without breaking the root app
βοΈ Maintainability β Clean separation of concerns
π Summary β Recap & Next Steps
AngularJS NgModules and configuration patterns allow you to build structured, testable, and scalable applications. By breaking your app into modules and leveraging .config()
and .run()
blocks, you gain fine control over initialization, routing, dependency setup, and runtime behavior.
π Key Takeaways:
- Declare modules using
angular.module()
- Use
.config()
for routing and provider setup - Use
.run()
for initialization logic - Inject sub-modules into the root app to organize features
βοΈ Real-world Relevance:
Large AngularJS applicationsβespecially dashboards, CMSs, and enterprise appsβdepend heavily on clean module configuration for long-term sustainability.
β FAQ β AngularJS NgModules & Configurations
β What is the difference between .config()
and .run()
in AngularJS?
β
.config()
is used to configure providers before the app runs, while .run()
executes logic after the app is initialized.
β Can I have multiple modules in one app?
β
Yes. It’s a best practice to split your app into feature, shared, and routing modules for better structure.
β Should I define constants in config or run?
β
Use .constant()
in a shared module, and access it in both .config()
and .run()
blocks.
β What happens if I inject a module twice?
β
AngularJS ignores duplicate module injections to prevent duplication or error.
Share Now :