Topic 13 Components, Modules, and Services
Topic 13 Components, Modules, and Services
Components in Angular
Components are the fundamental building blocks of Angular applications
They encapsulate a specific part of the user interface (UI) and its associated
behavior. Each component acts as a self-contained unit, managing its own logic and
presentation.
Benefits of components
02 Reusability
different parts of the application,
promoting code reuse and minimizing
duplication.
03 Isolation
reducing the risk of unintended
interactions with other parts of the
application.
Create a Component
OR
ng g c <component_name>
Basic Example on Angular component
Step 1: Create a component by using below command
ng g c first_task
Copy ‘app-textcomponent’
Step 4: go back to \src\app\app.component.html file and update
custom tag in the same file.
After updating this file
<p>Hello World</p> we can see
<app-textcomponent></app-textcomponent> first_task.component.ht
ml file data on
localhost
Using component in Angular
Go to component.html and component.css files and write the necessary
code in both of the files.
Modules in Angular are organizational units that group related components, directives, pipes,
and services.
App.module.ts file
Key Properties of an Angular Module
● The declarations array includes the components, directives,
Declaration and pipes that belong to the module.
● These are the building blocks that are scoped to the module.
declarations: [
AppComponent,
NewCmpComponent
]
Key Properties of an Angular Module
● The imports array lists other modules that this module depends
Imports on.
● Importing modules allows the components of the current
module to use components from other modules.
imports: [
BrowserModule,
FormsModule
]
Key Properties of an Angular Module
● The providers array contains services that the module
Providers contributes to the dependency injection system.
● These services are available for injection in the components of
the module.
A. Services
B. Components, directives, and pipes
C. External modules
D. Bootstrap components
B
Apply
In an Angular module, what does the 'bootstrap' property specify?
They provide a centralized place for features, data, and logic that are
independent of components.
Why do we need Services in Angular?
getData(): string[] {
return this.data;
}
ng serve
A) .html
B) .ts
C) .css
D) .json
B
Craete
Demonstrate how to create an Angular service using the Angular CLI.