SlideShare a Scribd company logo
www.webstackacademy.com
Components
Angular
www.webstackacademy.comwww.webstackacademy.com
Connecting the dots
(A Quick looking-back!)
www.webstackacademy.com
What we have done so far?
Introduction
Angular
Development
Environment
TypeScript
programming
Stepping into
Angular
framework
Why Angular?
SPA
Framework
Angular history
Linux platform,
Node, NPM
Angular CLI
Angular directory
Missing OOP features
Development Quality
Transpiler
We are HERE!
www.webstackacademy.comwww.webstackacademy.com
Components & Modules
(Key building blocks of Angular Application)
www.webstackacademy.com
What is a component?
• A component is anything visible to end user and can be reused many times within an app
• It controls a patch of screen called a view
• Component can intern can have multiple sub-components
• All components will have a beginning called root-component. From there components will
further have tree structure in a modular fashion
www.webstackacademy.com
Data
HTML
(Template)
Logic
Component
What does the component consist of?
www.webstackacademy.com
Why Component? - Benefits
• Modular: It makes the application more modular, in alignment with framework
• Data encapsulation: Data access via methods
• Re-usable: Makes the development more efficient
Analogy: Individual electronic (RLC) components
www.webstackacademy.com
What is a Module?
• Module is a container for a group related components,
one level above the component
• It also contains other parts of an app: Services, Directives,
Pipes (will see them one-by-one!)
• Every Angular app has at least one module, called root
module. Root module is conventionally named
AppModule
• Most apps have more than one modules (Root module +
Individual Feature modules)
Analogy: Individual electronic components, mounted together to give a meaningful
functionality (ex: WiFi, SSD etc…) called as “module”
www.webstackacademy.comwww.webstackacademy.com
Creating a Component - GUI
(Hooking your first piece of code into Angular)
www.webstackacademy.com
Creating a component – GUI mode
 Step-1: Creating / Adding your component TS file
• Open your Angular project folder (ex: myFirstApp) in your editor (ex: VS code). Go to src/app folder.
• Add a new file in the following format: <component_name>.component.ts (ex: courses.component.ts)
• Add the following lines into your new component file. Explanation are given as comments.
// Decorator class
import { Component } from '@angular/core'
// Selector is a custom HTML tag. Template has the HTML code
@Component({
selector: 'courses',
template: '<h2>My list of courses</h2>'
})
// Main class of component, you can define attributes and methods here...
export class CoursesComponent {
}
www.webstackacademy.com
Creating a component – GUI mode
 Step-2: Make your component as a part of the module (app.module.ts file)
 2.1 Make your component available for Module by importing it into the module
// Add your new component here by importing it
import { CoursesComponent } from './courses.component';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
www.webstackacademy.com
Creating a component – GUI mode
 Step-2: Make your component as a part of the module (app.module.ts file)
 Add your new component (ex: CoursesComponent) under the declarations sections of the module.
 Please note the modules contain more than components (ex: bootstrap), which we will study in further
chapters
@NgModule({
declarations: [
AppComponent,
CoursesComponent
],
imports: [
BrowserModule,
],
providers: [],
bootstrap: [AppComponent]
})
www.webstackacademy.com
Creating a component – GUI mode
Step-3: Go-to app.component.html file and add your selector (ex: <courses>
into this file). U can add any HTML code here.
Also ensure you delete previously existing HTML code to ensure the file looks
cleaner and easier to read.
Step-4: Save all files and run your Angular App again (ng serve --open).
U should be able to see your custom selectors displaying the corresponding
template code in the browser.
www.webstackacademy.comwww.webstackacademy.com
Creating a Component - CLI
(Hooking your first piece of code into Angular)
www.webstackacademy.com
Creating a component – CLI mode
• Sometimes you might find it little hard to create a component using GUI. In case you miss out
any steps, the app will not work.
• Angular CLI offers much more reliable way to create a component. Try out the following
command, which will not only generate a component but also make appropriate entries
$ ng g c command // Creates a new component - command
www.webstackacademy.com
Creating a component – CLI mode
www.webstackacademy.com
Creating a component – CLI mode
• Now you may see in your newly generated component, you have three files (HTML / CSS /
TS), the three important files to run a typical front end application
• Any changes you can make into the HTML file and include corresponding selector into
app.component.html to make this active
• By having multiple components mean there are multiple pieces coming together and
working in an app. This is just a beginning, so many things we can do on top of this.
www.webstackacademy.comwww.webstackacademy.com
Services
(De-coupling view and its functionality)
www.webstackacademy.com
What is a Service?
• In real-time web application, the component should display (present) the content using the
template (HTML)
• The content gets fetched from a server and make it available for a component, which should
be de-coupled from the component
• Only presentation is the component's responsibility, it should NOT be dealing with the data
• Hence data handling need to be delegated to a somebody, which is called as 'Service' in
Angular. The service primarily deals with an HTTP end-point (ex: RESTful interfaces / REST
APIs) from the server
• To demonstrate HTTP data fetching requires a service (which we will do in the Node.js
integration topic later). In the current topic let us create a service with a fake HTTP endpoint
www.webstackacademy.com
What is a Service? - Analogy
Analogy: Components & Modules only makes sense when a meaningful functionality is
achieved (ex: SSD display).
www.webstackacademy.comwww.webstackacademy.com
Creating a Service - GUI
(Giving some meaningful functionality for your component)
www.webstackacademy.com
Creating a service – GUI mode
 Step-1: Creating / Adding your service TS file
• Go to your src/app folder.
• Add a new file in the following format: <component_name>.service.ts (ex: courses.service.ts)
• Add the following lines into your new service. Explanation are given as comments
• Unlike component, no decorator class is required here. This is a plain TypeScript class
export class CoursesService {
getCourseList()
{
// Create an array for courses and return
var courseList = ["FullStack dev", "FrontEnd dev" , "Backend dev"];
return courseList;
}
}
www.webstackacademy.com
Creating a service – GUI mode
 Step-2: List this new services as a dependency for my component (app.module.ts file)
import { CoursesService } from './courses.service';
@NgModule({
declarations: [
AppComponent,
CommandComponent,
CoursesComponent
],
imports: [
BrowserModule,
],
providers: [
CoursesService
],
bootstrap: [AppComponent]
})
List your required service
www.webstackacademy.com
Creating a service – GUI mode
 Step-3: Make changes in your component file (courses.component.ts)
// Import a service
import { CoursesService } from './courses.service';
@Component({
selector: 'courses',
template: `
<h2> {{title}}</h2>
<ul> {{courses}}</ul>
`
})
export class CoursesComponent {
title = 'List of courses in WSA:';
courses;
constructor (service: CoursesService)
{
this.courses = service.getCourseList();
}
}
These changes can be done in the
HTML file pointed by templateURL
as well
www.webstackacademy.comwww.webstackacademy.com
Creating a Service - CLI
(Using CLI to generate your services)
www.webstackacademy.com
Creating a service – CLI mode
Similar to component, services can be added to a component using CLI. Along with generation it will
make appropriate entries into other files
$ ng g s emailservice // Creates a new service
www.webstackacademy.com
Creating a component – CLI mode
www.webstackacademy.com
Dependency Injection
• Dependency Injection (DI, one of the application design pattern) is a way to create objects
that depend upon other objects.
• One object supplies the dependencies of another object. A dependency is an object that can
be used (a service).
• An injection is the passing of a dependency to a dependent object that would use it, rather
than allowing a client to build or find the service, is the fundamental requirement of the
pattern.
• This allows the component to make acquiring dependencies someone else's problem (in our
case it is the Angular framework).
• The intent behind dependency injection is to decouple objects to the extent that no client
code has to be changed simply because an object it depends on needs to be changed to a
different one.
www.webstackacademy.com
Exercise
• Assume you are developing a simple e-commerce application. Which will fetch following
information from the server and display them in a tabular format in the browser:
• Item name (Pen, iPhone 10, Levis jean)
• Item category (Stationary, Electronics, Apparels)
• Item price (100, 100000, 1500)
• Implement the above functionality using Angular with following way:
• Create a new project
• Create a new component & service (CLI usage is recommended)
• The service should have three methods:
• getItemName()
• getItemCategory()
• getItemPrice()
• All methods to simulate a fake HTTP endpoint (by returning static arrays)
• Assume the number of items are fixed (later we can make it dynamic)
www.webstackacademy.com
WebStack Academy
#83, Farah Towers,
1st Floor, MG Road,
Bangalore – 560001
M: +91-809 555 7332
E: training@webstackacademy.com
WSA in Social Media:

More Related Content

PPT
Introduction to MongoDB
Ravi Teja
 
PPTX
Angular modules in depth
Christoffer Noring
 
PPTX
Presentation on Cloud computing
Vijay Bhanu Thodupunoori
 
PDF
Introduction to Android Development
Aly Abdelkareem
 
PPTX
Kubernetes PPT.pptx
ssuser0cc9131
 
PPTX
Introduction to MERN Stack
Surya937648
 
PDF
Personas Demystified 1.0
Mo Goltz
 
PDF
Spring Framework - Spring Security
Dzmitry Naskou
 
Introduction to MongoDB
Ravi Teja
 
Angular modules in depth
Christoffer Noring
 
Presentation on Cloud computing
Vijay Bhanu Thodupunoori
 
Introduction to Android Development
Aly Abdelkareem
 
Kubernetes PPT.pptx
ssuser0cc9131
 
Introduction to MERN Stack
Surya937648
 
Personas Demystified 1.0
Mo Goltz
 
Spring Framework - Spring Security
Dzmitry Naskou
 

What's hot (20)

PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PPT
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
PDF
Angular components
Sultan Ahmed
 
PDF
Angular - Chapter 5 - Directives
WebStackAcademy
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PPTX
Angular 14.pptx
MohaNedGhawar
 
ODP
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
PPTX
Angular Data Binding
Jennifer Estrada
 
PPTX
Angular overview
Thanvilahari
 
PPTX
Angular 9
Raja Vishnu
 
PDF
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
PDF
Building blocks of Angular
Knoldus Inc.
 
PPTX
Introduction to angular with a simple but complete project
Jadson Santos
 
PPTX
Angular introduction students
Christian John Felix
 
PPT
Angular App Presentation
Elizabeth Long
 
PDF
Angular Directives
iFour Technolab Pvt. Ltd.
 
PPTX
Angular tutorial
Rohit Gupta
 
PPTX
What is Angular?
Albiorix Technology
 
PDF
Angular Advanced Routing
Laurent Duveau
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angular components
Sultan Ahmed
 
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
Angular 14.pptx
MohaNedGhawar
 
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
Angular Data Binding
Jennifer Estrada
 
Angular overview
Thanvilahari
 
Angular 9
Raja Vishnu
 
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Building blocks of Angular
Knoldus Inc.
 
Introduction to angular with a simple but complete project
Jadson Santos
 
Angular introduction students
Christian John Felix
 
Angular App Presentation
Elizabeth Long
 
Angular Directives
iFour Technolab Pvt. Ltd.
 
Angular tutorial
Rohit Gupta
 
What is Angular?
Albiorix Technology
 
Angular Advanced Routing
Laurent Duveau
 
Ad

Similar to Angular - Chapter 3 - Components (20)

PPTX
Angular kickstart slideshare
SaleemMalik52
 
PPTX
Angularjs2 presentation
dharisk
 
PPTX
Angular IO
Jennifer Estrada
 
PPTX
Valentine with Angular js - Introduction
Senthil Kumar
 
PPTX
UQ21CA642BA1-Unit-3-WAF-Class18-Introduction to Angular Routing.pptx
TamalDey28
 
PPTX
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
tilejak773
 
PDF
better-apps-angular-2-day1.pdf and home
ChethanGowda886434
 
PPTX
Web worker in your angular application
Suresh Patidar
 
PPTX
Angular Framework ppt for beginners and advanced
Preetha Ganapathi
 
PPTX
Angular Basics.pptx
AshokKumar616995
 
PDF
Angular 7 Firebase5 CRUD Operations with Reactive Forms
Digamber Singh
 
PPTX
Intoduction to Angularjs
Gaurav Agrawal
 
PDF
Angular2 with type script
Ravi Mone
 
PPT
introduction to Angularjs basics
Ravindra K
 
PPTX
mean stack
michaelaaron25322
 
PPTX
Angularj2.0
Mallikarjuna G D
 
PPTX
angularJs Workshop
Ran Wahle
 
PDF
Angular Interview Questions-PDF.pdf
JohnLeo57
 
PPTX
AngularJs Workshop SDP December 28th 2014
Ran Wahle
 
PPTX
Mvc summary
Muhammad Younis
 
Angular kickstart slideshare
SaleemMalik52
 
Angularjs2 presentation
dharisk
 
Angular IO
Jennifer Estrada
 
Valentine with Angular js - Introduction
Senthil Kumar
 
UQ21CA642BA1-Unit-3-WAF-Class18-Introduction to Angular Routing.pptx
TamalDey28
 
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
tilejak773
 
better-apps-angular-2-day1.pdf and home
ChethanGowda886434
 
Web worker in your angular application
Suresh Patidar
 
Angular Framework ppt for beginners and advanced
Preetha Ganapathi
 
Angular Basics.pptx
AshokKumar616995
 
Angular 7 Firebase5 CRUD Operations with Reactive Forms
Digamber Singh
 
Intoduction to Angularjs
Gaurav Agrawal
 
Angular2 with type script
Ravi Mone
 
introduction to Angularjs basics
Ravindra K
 
mean stack
michaelaaron25322
 
Angularj2.0
Mallikarjuna G D
 
angularJs Workshop
Ran Wahle
 
Angular Interview Questions-PDF.pdf
JohnLeo57
 
AngularJs Workshop SDP December 28th 2014
Ran Wahle
 
Mvc summary
Muhammad Younis
 
Ad

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
PDF
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
PDF
Webstack Academy - Internship Kick Off
WebStackAcademy
 
PDF
Building Your Online Portfolio
WebStackAcademy
 
PDF
Front-End Developer's Career Roadmap
WebStackAcademy
 
PDF
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
PDF
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PDF
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
PDF
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
PDF
JavaScript - Chapter 11 - Events
WebStackAcademy
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PDF
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
PDF
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
PDF
JavaScript - Chapter 5 - Operators
WebStackAcademy
 
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
JavaScript - Chapter 11 - Events
WebStackAcademy
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
JavaScript - Chapter 5 - Operators
WebStackAcademy
 

Recently uploaded (20)

PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Doc9.....................................
SofiaCollazos
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 

Angular - Chapter 3 - Components

  • 3. www.webstackacademy.com What we have done so far? Introduction Angular Development Environment TypeScript programming Stepping into Angular framework Why Angular? SPA Framework Angular history Linux platform, Node, NPM Angular CLI Angular directory Missing OOP features Development Quality Transpiler We are HERE!
  • 5. www.webstackacademy.com What is a component? • A component is anything visible to end user and can be reused many times within an app • It controls a patch of screen called a view • Component can intern can have multiple sub-components • All components will have a beginning called root-component. From there components will further have tree structure in a modular fashion
  • 7. www.webstackacademy.com Why Component? - Benefits • Modular: It makes the application more modular, in alignment with framework • Data encapsulation: Data access via methods • Re-usable: Makes the development more efficient Analogy: Individual electronic (RLC) components
  • 8. www.webstackacademy.com What is a Module? • Module is a container for a group related components, one level above the component • It also contains other parts of an app: Services, Directives, Pipes (will see them one-by-one!) • Every Angular app has at least one module, called root module. Root module is conventionally named AppModule • Most apps have more than one modules (Root module + Individual Feature modules) Analogy: Individual electronic components, mounted together to give a meaningful functionality (ex: WiFi, SSD etc…) called as “module”
  • 9. www.webstackacademy.comwww.webstackacademy.com Creating a Component - GUI (Hooking your first piece of code into Angular)
  • 10. www.webstackacademy.com Creating a component – GUI mode  Step-1: Creating / Adding your component TS file • Open your Angular project folder (ex: myFirstApp) in your editor (ex: VS code). Go to src/app folder. • Add a new file in the following format: <component_name>.component.ts (ex: courses.component.ts) • Add the following lines into your new component file. Explanation are given as comments. // Decorator class import { Component } from '@angular/core' // Selector is a custom HTML tag. Template has the HTML code @Component({ selector: 'courses', template: '<h2>My list of courses</h2>' }) // Main class of component, you can define attributes and methods here... export class CoursesComponent { }
  • 11. www.webstackacademy.com Creating a component – GUI mode  Step-2: Make your component as a part of the module (app.module.ts file)  2.1 Make your component available for Module by importing it into the module // Add your new component here by importing it import { CoursesComponent } from './courses.component'; import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core';
  • 12. www.webstackacademy.com Creating a component – GUI mode  Step-2: Make your component as a part of the module (app.module.ts file)  Add your new component (ex: CoursesComponent) under the declarations sections of the module.  Please note the modules contain more than components (ex: bootstrap), which we will study in further chapters @NgModule({ declarations: [ AppComponent, CoursesComponent ], imports: [ BrowserModule, ], providers: [], bootstrap: [AppComponent] })
  • 13. www.webstackacademy.com Creating a component – GUI mode Step-3: Go-to app.component.html file and add your selector (ex: <courses> into this file). U can add any HTML code here. Also ensure you delete previously existing HTML code to ensure the file looks cleaner and easier to read. Step-4: Save all files and run your Angular App again (ng serve --open). U should be able to see your custom selectors displaying the corresponding template code in the browser.
  • 14. www.webstackacademy.comwww.webstackacademy.com Creating a Component - CLI (Hooking your first piece of code into Angular)
  • 15. www.webstackacademy.com Creating a component – CLI mode • Sometimes you might find it little hard to create a component using GUI. In case you miss out any steps, the app will not work. • Angular CLI offers much more reliable way to create a component. Try out the following command, which will not only generate a component but also make appropriate entries $ ng g c command // Creates a new component - command
  • 17. www.webstackacademy.com Creating a component – CLI mode • Now you may see in your newly generated component, you have three files (HTML / CSS / TS), the three important files to run a typical front end application • Any changes you can make into the HTML file and include corresponding selector into app.component.html to make this active • By having multiple components mean there are multiple pieces coming together and working in an app. This is just a beginning, so many things we can do on top of this.
  • 19. www.webstackacademy.com What is a Service? • In real-time web application, the component should display (present) the content using the template (HTML) • The content gets fetched from a server and make it available for a component, which should be de-coupled from the component • Only presentation is the component's responsibility, it should NOT be dealing with the data • Hence data handling need to be delegated to a somebody, which is called as 'Service' in Angular. The service primarily deals with an HTTP end-point (ex: RESTful interfaces / REST APIs) from the server • To demonstrate HTTP data fetching requires a service (which we will do in the Node.js integration topic later). In the current topic let us create a service with a fake HTTP endpoint
  • 20. www.webstackacademy.com What is a Service? - Analogy Analogy: Components & Modules only makes sense when a meaningful functionality is achieved (ex: SSD display).
  • 21. www.webstackacademy.comwww.webstackacademy.com Creating a Service - GUI (Giving some meaningful functionality for your component)
  • 22. www.webstackacademy.com Creating a service – GUI mode  Step-1: Creating / Adding your service TS file • Go to your src/app folder. • Add a new file in the following format: <component_name>.service.ts (ex: courses.service.ts) • Add the following lines into your new service. Explanation are given as comments • Unlike component, no decorator class is required here. This is a plain TypeScript class export class CoursesService { getCourseList() { // Create an array for courses and return var courseList = ["FullStack dev", "FrontEnd dev" , "Backend dev"]; return courseList; } }
  • 23. www.webstackacademy.com Creating a service – GUI mode  Step-2: List this new services as a dependency for my component (app.module.ts file) import { CoursesService } from './courses.service'; @NgModule({ declarations: [ AppComponent, CommandComponent, CoursesComponent ], imports: [ BrowserModule, ], providers: [ CoursesService ], bootstrap: [AppComponent] }) List your required service
  • 24. www.webstackacademy.com Creating a service – GUI mode  Step-3: Make changes in your component file (courses.component.ts) // Import a service import { CoursesService } from './courses.service'; @Component({ selector: 'courses', template: ` <h2> {{title}}</h2> <ul> {{courses}}</ul> ` }) export class CoursesComponent { title = 'List of courses in WSA:'; courses; constructor (service: CoursesService) { this.courses = service.getCourseList(); } } These changes can be done in the HTML file pointed by templateURL as well
  • 25. www.webstackacademy.comwww.webstackacademy.com Creating a Service - CLI (Using CLI to generate your services)
  • 26. www.webstackacademy.com Creating a service – CLI mode Similar to component, services can be added to a component using CLI. Along with generation it will make appropriate entries into other files $ ng g s emailservice // Creates a new service
  • 28. www.webstackacademy.com Dependency Injection • Dependency Injection (DI, one of the application design pattern) is a way to create objects that depend upon other objects. • One object supplies the dependencies of another object. A dependency is an object that can be used (a service). • An injection is the passing of a dependency to a dependent object that would use it, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern. • This allows the component to make acquiring dependencies someone else's problem (in our case it is the Angular framework). • The intent behind dependency injection is to decouple objects to the extent that no client code has to be changed simply because an object it depends on needs to be changed to a different one.
  • 29. www.webstackacademy.com Exercise • Assume you are developing a simple e-commerce application. Which will fetch following information from the server and display them in a tabular format in the browser: • Item name (Pen, iPhone 10, Levis jean) • Item category (Stationary, Electronics, Apparels) • Item price (100, 100000, 1500) • Implement the above functionality using Angular with following way: • Create a new project • Create a new component & service (CLI usage is recommended) • The service should have three methods: • getItemName() • getItemCategory() • getItemPrice() • All methods to simulate a fake HTTP endpoint (by returning static arrays) • Assume the number of items are fixed (later we can make it dynamic)
  • 30. www.webstackacademy.com WebStack Academy #83, Farah Towers, 1st Floor, MG Road, Bangalore – 560001 M: +91-809 555 7332 E: [email protected] WSA in Social Media: