Learn NgModule in Angular with Examples

Fireship · Beginner ·💻 AI-Assisted Coding ·8y ago

Key Takeaways

The video covers the basics of NgModule in Angular, including its purpose, metadata, and how to use it to organize an application. It also provides examples of different types of modules, such as feature modules, shared modules, and core modules.

Full Transcript

in this episode we're going to talk about ng module and how to properly use it to organize an angular application so what's the purpose of an ng module in the first place the answer is it helps organize the complex relationship between views such as components and directives to data providers such as services and guards it works by allowing you to define all these relationships in the Swingle file as opposed to doing it individually and every component service as was the case in early days of angular 2 now let's look inside an NG module and see what these metadata terms actually mean first we have declarations which is where you would put any component directive or pipe and then it'll be privately available to that module if you want it to be publicly available to other modules then you'll need to add it to the export section this concept is similar to public and private functions and object-oriented programming components will be private by default and become public if you add them to exports other modules gain access to exported components by importing that module in their own metadata so just to recap declarations imports and exports define the relationship between components and how they're shared between different ng modules next we have the bootstrap property which defines a component that's used to initially load your application by default this will usually be the app component which is created automatically by the angular CLI the final property you should be aware of is providers this is where you register data providers such as Services and guards that can be injected into components within this module the injectable class will be available to any component in this module as well as any module that imports it now let's look at some practical ways we can use energy module to organize an application in this example we have all of our components and services in the group app module that was created by default as our app grows larger this file is going to be more and more cluttered and more and more hard to maintain our goal is to refactor most of our components and services to their own modules with the exception of the app component which we still want to bootstrap from the root module overall this will keep the logic in our app much more organized now let's take a look at the routing module that was created by default by the angular CLI a routing module should have no declarations it may have providers in the form of guards and it should always export the router module we can add our contact list component to the router and it will load that component imperative way when the route is activated which in this case is just route URL depending on the complexity of your app you may use multiple routing modules for better organization as well as lazy loading now let's build a feature module to organize everything related to contacts in our app feature modules will consist mostly of declarations and possibly a provider but they should never export anything their primary purpose is to organize business logic related to a specific feature we generate the module with the angular CLI and copy everything related to contacts over to it notice we're also importing the common module that gives us access to angular's most common features such as NGS and 10g for now we can go back to the route and delete everything related to contacts then import the contact module and add it to the imports in the route if we had 20 different components related to contacts you can see how this would greatly improve the clutter and the file now let's look at a shared module also called widget modules they're almost identical to future modules except that they add all their declarations to the exports as well so they can be distributed to other module common use cases include social media icons Oh Dean spinners and other UI elements similar to those let's generate the module from the CLI then add the social icons component to both the declarations and the exports from there we can go ahead and import it in both the root module as well as the feature module we created in the previous step now the social icons component will be available everywhere in the app the last type we'll look at is a core or service module it has no declarations and only focuses on services for dependency injection common providers to include here would be an off service or a toast message service anything that provides data globally throughout the app in this example we're extracting our off service into the core module we generate it with the CLI and copy over the auth service to a new home we can also remove the common module since we're all made dealing with services here back in the root we can go ahead and import the core module and add it to the imports with the others if we look over our app launch well now we can see it's a lot more organized we just have the one declaration no providers the one bootstrap component and just a handful of modules to manage that covers the main patterns you'll see with ng module and angular applications in an upcoming video I'll show you how to use the router along with ng modules to lazy load component that's it for this episode if you found the video helpful please like and subscribe and if you want to support the channel consider becoming a pro subscriber at angular firebase comm for just a few bucks a month you'll get access to your exclusive content as well as free one on one project consulting thanks for watching and I'll see you next time [Music] you

Original Description

Full lesson https://angularfirebase.com/lessons/a-simple-explanation-of-ngmodule/ In this episode, I show you how to refactor an Angular app into different modules for better organization and maintainability. Full NgModule FAQ https://angular.io/guide/ngmodule-faq
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fireship · Fireship · 22 of 60

1 Angular 4 Development and Production Environments with Firebase
Angular 4 Development and Production Environments with Firebase
Fireship
2 OAuth with Angular and Firebase Tutorial
OAuth with Angular and Firebase Tutorial
Fireship
3 Anonymous Authentication with Angular and Firebase - Lazy Registration
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
4 Angular Router Guards for Firebase Users
Angular Router Guards for Firebase Users
Fireship
5 Angular Firebase CRUD App with NoSQL Database Tutorial
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
6 Upload Files from Angular to Firebase Storage
Upload Files from Angular to Firebase Storage
Fireship
7 How to Deploy an Angular App to Firebase Hosting
How to Deploy an Angular App to Firebase Hosting
Fireship
8 Sharing Data between Components in Angular
Sharing Data between Components in Angular
Fireship
9 Loading Spinners for Asynchronous Firebase Data
Loading Spinners for Asynchronous Firebase Data
Fireship
10 Angular 4 Transactional Email with Google Firebase Cloud Functions
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
11 Firebase Database Rules Tutorial
Firebase Database Rules Tutorial
Fireship
12 Autocomplete Search with Angular4 and Firebase
Autocomplete Search with Angular4 and Firebase
Fireship
13 Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
14 Angular Drag-and-Drop File Uploads to Firebase Storage
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
15 Text Translation with Firebase Cloud Functions onWrite and Angular 4
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
16 Custom Usernames with Firebase Authentication
Custom Usernames with Firebase Authentication
Fireship
17 Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
18 Simple Pagination with Firebase and Angular 4
Simple Pagination with Firebase and Angular 4
Fireship
19 How to Connect Firebase Users to their Data - 3 Methods
How to Connect Firebase Users to their Data - 3 Methods
Fireship
20 Add Toast Message Notifications to your Angular App
Add Toast Message Notifications to your Angular App
Fireship
21 Facebook-Inspired Reactions System with Angular and Firebase
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
Learn NgModule in Angular with Examples
Learn NgModule in Angular with Examples
Fireship
23 Lazy Loading Components in Angular 4
Lazy Loading Components in Angular 4
Fireship
24 Stripe Checkout Payments with Angular and Firebase - Part 1
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
25 Process Stripe Payments with Firebase Cloud Functions - Part 2
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
26 Selling Digital Content in Angular with Stripe Payments - Part 3
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
27 Angular 4 Full Text Search with Algolia - Part 1
Angular 4 Full Text Search with Algolia - Part 1
Fireship
28 Algolia with Firebase Cloud Functions - Part 2
Algolia with Firebase Cloud Functions - Part 2
Fireship
29 Firebase Phone Authentication in Angular 4
Firebase Phone Authentication in Angular 4
Fireship
30 Top 7 RxJS Concepts for Angular Developers
Top 7 RxJS Concepts for Angular Developers
Fireship
31 Learn Angular Animations with 5 Examples
Learn Angular Animations with 5 Examples
Fireship
32 Advanced Firebase Data Filtering (Multi-Property)
Advanced Firebase Data Filtering (Multi-Property)
Fireship
33 Realtime Maps with Mapbox + Firebase + Angular
Realtime Maps with Mapbox + Firebase + Angular
Fireship
34 Angular Reactive Forms with Firebase Database Backend
Angular Reactive Forms with Firebase Database Backend
Fireship
35 Send Push Notifications in Angular with Firebase Cloud Messaging
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
36 Top 7 Ways to Debug Angular 4 Apps
Top 7 Ways to Debug Angular 4 Apps
Fireship
37 Infinite Scroll with Angular and Firebase
Infinite Scroll with Angular and Firebase
Fireship
38 Use TypeScript with Firebase Cloud Functions
Use TypeScript with Firebase Cloud Functions
Fireship
39 Realtime Graphs and Charts with Plotly and Firebase
Realtime Graphs and Charts with Plotly and Firebase
Fireship
40 Role-Based User Permissions in Firebase
Role-Based User Permissions in Firebase
Fireship
41 User Presence System in Realtime - Online, Offline, Away
User Presence System in Realtime - Online, Offline, Away
Fireship
42 Location-based Queries with GeoFire and Angular Google Maps
Location-based Queries with GeoFire and Angular Google Maps
Fireship
43 Angular ngrx Redux Quick Start Tutorial
Angular ngrx Redux Quick Start Tutorial
Fireship
44 Angular Ngrx Effects with Firebase Database
Angular Ngrx Effects with Firebase Database
Fireship
45 Progressive Web Apps with Angular
Progressive Web Apps with Angular
Fireship
46 Angular Ngrx with Firebase Google OAuth User Authentication
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
47 RxJS Quick Start with Practical Examples
RxJS Quick Start with Practical Examples
Fireship
48 Send SMS Text Messages with Twilio and Firebase
Send SMS Text Messages with Twilio and Firebase
Fireship
49 Firebase Database Performance Profiling
Firebase Database Performance Profiling
Fireship
50 Native Desktop Apps with Angular and Electron
Native Desktop Apps with Angular and Electron
Fireship
51 Subscription Payments with Stripe, Angular, and Firebase
Subscription Payments with Stripe, Angular, and Firebase
Fireship
52 Firestore with AngularFire5 Quick Start Tutorial
Firestore with AngularFire5 Quick Start Tutorial
Fireship
53 Angular HTTP Client Quick Start Tutorial
Angular HTTP Client Quick Start Tutorial
Fireship
54 Google Sign-In with Firestore Custom User Data
Google Sign-In with Firestore Custom User Data
Fireship
55 Star Review System from Scratch with Firestore + Angular
Star Review System from Scratch with Firestore + Angular
Fireship
56 Angular Chatbot with Dialogflow (API.ai)
Angular Chatbot with Dialogflow (API.ai)
Fireship
57 Learn @ngrx/entity and Feature Modules
Learn @ngrx/entity and Feature Modules
Fireship
58 Infinite Scroll Pagination with Firestore
Infinite Scroll Pagination with Firestore
Fireship
59 Faster Firestore via Data Aggregation
Faster Firestore via Data Aggregation
Fireship
60 Contentful - CMS for Angular Progressive Web Apps
Contentful - CMS for Angular Progressive Web Apps
Fireship

This video teaches how to use NgModule to organize Angular applications, including creating feature modules, shared modules, and core modules. It covers the basics of NgModule metadata and how to use it to improve code maintainability.

Key Takeaways
  1. Create a new NgModule using the Angular CLI
  2. Define metadata for the module, including declarations, imports, and exports
  3. Create feature modules to organize business logic
  4. Use shared modules to distribute UI elements
  5. Create core modules for services and dependency injection
  6. Import and use modules in the application
💡 NgModule is a powerful tool for organizing Angular applications, and using it effectively can improve code maintainability and scalability.

Related AI Lessons

Up next
Azure Security Priorities for 2026: Identity, Governance, AI Security & Zero Trust
Valto Microsoft Specialists
Watch →