in Technology by
What are the Components, Modules and Services in Angular?

1 Answer

0 votes
by

to create an Angular application by running the following inside the command terminal:

ng new angularApp

The above command will create an angular application in the directory.
Next, let's move on to understand Components, Modules, and Services.

Components
In Angular, components are the basic building blocks, which control a part of the UI for any application.
A component is defined using the @Component decorator. Every component consists of three parts, the template which loads the view for the component, a stylesheet which defines the look and feel for the component, and a class that contains the business logic for the component.
For creating a component, inside the command terminal, navigate to the directory of the application created, and run the following command:

ng generate component test

Or

ng g c test

One can see the generated component inside src/app/test folder. The component will be defined inside test.component.ts and this is how it looks:

    
      import { Component, OnInit } from '@angular/core';

      @Component({
        selector: 'app-test',
        templateUrl: './test.component.html',
        styleUrls: ['./test.component.css']
      })
      export lass TestComponent implements OnInit {

        constructor() {}

        ngOnInit() {
        }
      }
    
  

As we can see in the above image, our component is defined with @Component decorator.

Modules
A module is a place where we can group components, directives, services, and pipes. Module decides whether the components, directives, etc can be used by other modules, by exporting or hiding these elements. Every module is defined with a @NgModule decorator.
By default, modules are of two types:

  • Root Module
  • Feature Module

Every application can have only one root module whereas, it can have one or more feature modules.
A root module imports BrowserModule, whereas a feature module imports CommonModule.

In the application that we created before, one can see that the root module is defined inside app.module.ts and this is how it looks:

    
      import { BrowserModule } from '@angular/platform-browser';
      import { NgModule } from '@angular/core';

      import { AppComponent } from './app.component';
      import { TestComponent } from './test/text.component';

      @NgModule({
        declarations: [
          AppComponent,
          TestComponent
        ],
        imports: [
          BrowserModule
        ],
        providers: [],
        bootstrap: [AppComponent]
      })
      export class AppModule { }
    
  

We can see in the above image that the component we created earlier is already imported in the declarations array.

To create a feature module, run the following command:

ng g m test-module

The module is created inside the src/app/test-module/test-module.module.ts file:

    
      import { NgModule } from '@angular/core';
      import { CommonModule } from '@angular/common';

      @NgModule({
        declarations: [],
        imports: [
          CommonModule
        ]
      })
      export class TestModuleModule { }
    
  

As one can see, CommonModule is imported since this is a feature module.

Services Services are objects which get instantiated only once during the lifetime of an application. The main objective of a service is to share data, functions with different components of an Angular application.
A service is defined using a @Injectable decorator. A function defined inside a service can be invoked from any component or directive.

To create a service, run the following command:

ng g s test-service

The service will be created inside src/app/test-service.service.ts:

    
      import { Injectable } from '@angular/core';

      @Injectable({
        providedIn: 'root'
      })
      export class TestServiceService {

        constructor() { }

      }
    
  

Any method/function defined inside the TestServiceService class can be directly used inside any component by just importing the service.

Related questions

0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    How does one share data between components in Angular?...
asked Jun 30, 2021 in Technology by JackTerrance
0 votes
    What is the need of Angular 8 components?...
asked Jun 28, 2021 in Technology by JackTerrance
0 votes
    I need to wrap a mat-slide-toggle component on my own, I wrote: mytoggle.component.ts import { ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm trying to create an Angular reusable button component. Right now, it is used like so: [label] ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Modules are temporarily stored in the nodes and ineteract with the control machine through a ______ protocol over the standard output. (1)XML (2)JSON...
asked Jul 5, 2021 in Technology by JackTerrance
0 votes
    What are modules and packages in Python?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    How many modules are there in Ansible?...
asked Jan 29, 2023 in Technology by JackTerrance
0 votes
    What are Ansible modules in detail?...
asked Jul 28, 2021 in Technology by JackTerrance
0 votes
    What are SAP Basics feature Configuration of the SAP modules?...
asked Jan 4, 2021 in Technology by JackTerrance
0 votes
    What are the different types of modules in Oracle forms?...
asked Dec 18, 2020 in Technology by JackTerrance
+1 vote
    Which are the modules of Web layer in Spring? A - WebSocket, Servlet, Web, Portlet B - WebSocket, Servlet, Web-MVC, Web C - HTML, JSP, WEB, Portlet D - HTML, Servlet, WEB, Portlet...
asked Oct 14, 2020 in Technology by JackTerrance
+1 vote
    Which are the modules of Data Access/ integration layer in Spring? A - JDBC, ORM, OXM, JMS, Transactions B - JDBC, ORM, OXM, JMS C - JDBC, ORM, Web, Beans D - JDBC, ORM, OXM, JMS...
asked Oct 14, 2020 in Technology by JackTerrance
+1 vote
    Which are the modules of core container in Spring? A - Beans, Core, Context, SpEL B - Core, Context, ORM, Web C - Core, Context, Aspects, Test D - Bean, Core, Context, Test...
asked Oct 14, 2020 in Technology by JackTerrance
...