SlideShare a Scribd company logo
1 of 12
HTTP AND OBSERVABLES IN
ANGULAR
BY: ABHISHEK PRASAD
210303130027(CC)
INTRODUCTION TO ANGULAR
What is Angular?
• A platform and framework for building single-page client applications using HTML
and TypeScript.
• Developed and maintained by Google, Angular integrates a range of features like
routing, forms management, and client-server communication.
CORE FEATURES OF ANGULAR:
• Component-Based Architecture: Angular structures applications as a tree of interconnected
components, each encapsulating elements of the UI.
• TypeScript-Based: Offers improved tooling, predictable static typing, and enhanced navigation and
refactoring services1.
• Data Binding: Facilitates the synchronization between the DOM and the component’s logic2.
• Dependency Injection: Angular’s powerful DI framework provides services to components, allowing for
greater modularity and reusability2.
• Directives: Extend HTML with new attributes called directives, which are great for encapsulating UI
behaviors2.
• Routing & Navigation: The Angular Router enables navigation from one view to the next as users
perform application tasks
UNDERSTANDING HTTP IN ANGULAR
HTTP in Web Applications
• HTTP (Hypertext Transfer Protocol) is the foundation of data
communication for the web, used for fetching HTML pages, files, or
data.
• In Angular, HTTP communication is handled through the HttpClient
module, which provides a way to perform HTTP requests and
handle responses.
UNDERSTANDING HTTP IN ANGULAR
Angular’s HttpClient
• A service that allows making HTTP requests to fetch or save data.
• Returns Observables to handle asynchronous operations, providing advantages like
cancellability and retry logic.
Making HTTP Requests
• Use HttpClient.get() to fetch data from a server.
• The method sends an HTTP request and returns an Observable that emits the requested data
when the response is received
UNDERSTANDING HTTP IN ANGULAR
Request Configuration
• Configure requests with headers, query parameters, and options for observing the
response or reporting progress.
• Important options include observe and responseType properties, which specify how much
of the response to return and the desired format of the returned data
Handling JSON Data
• Applications often request JSON data from servers.
• Angular’s HttpClient simplifies fetching JSON data, with most get() calls not requiring
additional options since JSON is the default data format
OBSERVABLES IN ANGULAR
What are Observables?
• A method for managing asynchronous data streams and events in Angular.
• They are part of the RxJS library and are used extensively within Angular
applications.
Key Features of Observables:
• Asynchronous Operations: Handle HTTP requests, user inputs, and events.
• Multiple Values Over Time: Emit zero or more values and handle them as a stream.
• Cancellation: Subscriptions to observables can be canceled, unlike promises.
• Operators: Apply functions to each item emitted by an observable.
OBSERVABLES IN ANGULAR
Usage in Angular
• HTTP Client: Angular’s HttpClient uses observables for handling AJAX requests and
responses.
• EventEmitter: Components communicate with each other using EventEmitter, which is an
observable that emits events.
• Async Pipe: Automatically subscribes to observables and updates the view with the latest
emitted values.
Advantages Over Promises
• More powerful and flexible, especially for complex data flows and multiple values.
• Better error handling and the ability to retry operations.
HTTP AND OBSERVABLES IN ANGULAR
Synergy of HTTP and Observables:
• Angular’s HttpClient uses Observables to handle HTTP requests, providing a robust framework for managing
asynchronous data.
Making HTTP Requests with Observables:
• Use HttpClient methods like get, post, put, and delete to interact with a server.
• These methods return Observables, allowing you to subscribe and handle the emitted data when the request is
completed.
Benefits of This Approach:
• Streamlined Error Handling: Use operators like catchError to manage errors gracefully.
• Cancellation: Cancel HTTP requests easily if they are no longer needed.
• Retry Logic: Implement retry strategies for failed HTTP requests using the retry operator.
FETCHING DATA WITH HTTP AND OBSERVABLES
Objective:
• Demonstrate how to fetch and display data from a server using Angular’s HttpClient and Observables.
Service Creation:
• Define a service that uses HttpClient to make a GET request.
• The service returns an Observable that emits the data when the response is received.
Component Subscription:
• In the component, subscribe to the Observable returned by the service.
• Assign the emitted data to a variable for display.
ERROR HANDLING IN HTTP REQUESTS
• Importance of Error Handling: Robust error handling is essential for a resilient Angular
application, ensuring a smooth user experience even when things go wrong.
• Using catchError Operator: The catchError operator is pivotal for handling errors in
Observable streams, particularly in HTTP requests made with Angular’s HttpClient.
• Centralizing Error Handling: Consider creating a global error-handling service that can be
used across different components and services for consistency and maintainability.
• Providing Meaningful Error Messages: When an error occurs, provide clear and actionable
feedback to the user, without exposing sensitive details.
• Logging Errors: Implement a logging strategy to capture errors for debugging and
monitoring application health.
THANK YOU

More Related Content

Similar to Http and observables in Angular .pptx

Single Page Applications in SharePoint with Angular
Single Page Applications in SharePoint with AngularSingle Page Applications in SharePoint with Angular
Single Page Applications in SharePoint with AngularSparkhound Inc.
 
Displaying google maps in mobileapplication.pptx
Displaying google maps in mobileapplication.pptxDisplaying google maps in mobileapplication.pptx
Displaying google maps in mobileapplication.pptxsanaiftikhar23
 
A Real-Time collaboration Platform for Construction company
A Real-Time collaboration Platform for Construction companyA Real-Time collaboration Platform for Construction company
A Real-Time collaboration Platform for Construction companyHemang Rindani
 
Angular js Online Training
Angular js Online TrainingAngular js Online Training
Angular js Online TrainingLearntek1
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
Angular crash course
Angular crash courseAngular crash course
Angular crash courseBirhan Nega
 
AngularJs (1.x) Presentation
AngularJs (1.x) PresentationAngularJs (1.x) Presentation
AngularJs (1.x) PresentationRaghubir Singh
 
Single page application 02
Single page application   02Single page application   02
Single page application 02Ismaeel Enjreny
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesWebStackAcademy
 
Taking Akka Streams & Akka Http to Large Scale Production Applications
Taking Akka Streams & Akka Http to Large Scale Production ApplicationsTaking Akka Streams & Akka Http to Large Scale Production Applications
Taking Akka Streams & Akka Http to Large Scale Production ApplicationsAkara Sucharitakul
 
Mastering Angular: A Comprehensive Guide
Mastering Angular: A Comprehensive GuideMastering Angular: A Comprehensive Guide
Mastering Angular: A Comprehensive GuideJay Verma
 
Migrating from a monolith to microservices – is it worth it?
Migrating from a monolith to microservices – is it worth it?Migrating from a monolith to microservices – is it worth it?
Migrating from a monolith to microservices – is it worth it?Katherine Golovinova
 
Angular presentation
Angular presentationAngular presentation
Angular presentationmerlihan
 

Similar to Http and observables in Angular .pptx (20)

Single Page Applications in SharePoint with Angular
Single Page Applications in SharePoint with AngularSingle Page Applications in SharePoint with Angular
Single Page Applications in SharePoint with Angular
 
Displaying google maps in mobileapplication.pptx
Displaying google maps in mobileapplication.pptxDisplaying google maps in mobileapplication.pptx
Displaying google maps in mobileapplication.pptx
 
A Real-Time collaboration Platform for Construction company
A Real-Time collaboration Platform for Construction companyA Real-Time collaboration Platform for Construction company
A Real-Time collaboration Platform for Construction company
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
Angular js Online Training
Angular js Online TrainingAngular js Online Training
Angular js Online Training
 
OSGi compendium
OSGi compendiumOSGi compendium
OSGi compendium
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Angular crash course
Angular crash courseAngular crash course
Angular crash course
 
Angular js for Beginnners
Angular js for BeginnnersAngular js for Beginnners
Angular js for Beginnners
 
AngularJs (1.x) Presentation
AngularJs (1.x) PresentationAngularJs (1.x) Presentation
AngularJs (1.x) Presentation
 
Angular js 1.3 basic tutorial
Angular js 1.3 basic tutorialAngular js 1.3 basic tutorial
Angular js 1.3 basic tutorial
 
Single page application 02
Single page application   02Single page application   02
Single page application 02
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Introduction to Angular JS
Introduction to Angular JSIntroduction to Angular JS
Introduction to Angular JS
 
Taking Akka Streams & Akka Http to Large Scale Production Applications
Taking Akka Streams & Akka Http to Large Scale Production ApplicationsTaking Akka Streams & Akka Http to Large Scale Production Applications
Taking Akka Streams & Akka Http to Large Scale Production Applications
 
Mastering Angular: A Comprehensive Guide
Mastering Angular: A Comprehensive GuideMastering Angular: A Comprehensive Guide
Mastering Angular: A Comprehensive Guide
 
Migrating from a monolith to microservices – is it worth it?
Migrating from a monolith to microservices – is it worth it?Migrating from a monolith to microservices – is it worth it?
Migrating from a monolith to microservices – is it worth it?
 
Angular presentation
Angular presentationAngular presentation
Angular presentation
 
Building Aneka clouds.ppt
Building Aneka clouds.pptBuilding Aneka clouds.ppt
Building Aneka clouds.ppt
 

Recently uploaded

Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...ScyllaDB
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptxFIDO Alliance
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxFIDO Alliance
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...marcuskenyatta275
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctBrainSell Technologies
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Skynet Technologies
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe中 央社
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...panagenda
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfAnubhavMangla3
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfalexjohnson7307
 

Recently uploaded (20)

Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 

Http and observables in Angular .pptx

  • 1. HTTP AND OBSERVABLES IN ANGULAR BY: ABHISHEK PRASAD 210303130027(CC)
  • 2. INTRODUCTION TO ANGULAR What is Angular? • A platform and framework for building single-page client applications using HTML and TypeScript. • Developed and maintained by Google, Angular integrates a range of features like routing, forms management, and client-server communication.
  • 3. CORE FEATURES OF ANGULAR: • Component-Based Architecture: Angular structures applications as a tree of interconnected components, each encapsulating elements of the UI. • TypeScript-Based: Offers improved tooling, predictable static typing, and enhanced navigation and refactoring services1. • Data Binding: Facilitates the synchronization between the DOM and the component’s logic2. • Dependency Injection: Angular’s powerful DI framework provides services to components, allowing for greater modularity and reusability2. • Directives: Extend HTML with new attributes called directives, which are great for encapsulating UI behaviors2. • Routing & Navigation: The Angular Router enables navigation from one view to the next as users perform application tasks
  • 4. UNDERSTANDING HTTP IN ANGULAR HTTP in Web Applications • HTTP (Hypertext Transfer Protocol) is the foundation of data communication for the web, used for fetching HTML pages, files, or data. • In Angular, HTTP communication is handled through the HttpClient module, which provides a way to perform HTTP requests and handle responses.
  • 5. UNDERSTANDING HTTP IN ANGULAR Angular’s HttpClient • A service that allows making HTTP requests to fetch or save data. • Returns Observables to handle asynchronous operations, providing advantages like cancellability and retry logic. Making HTTP Requests • Use HttpClient.get() to fetch data from a server. • The method sends an HTTP request and returns an Observable that emits the requested data when the response is received
  • 6. UNDERSTANDING HTTP IN ANGULAR Request Configuration • Configure requests with headers, query parameters, and options for observing the response or reporting progress. • Important options include observe and responseType properties, which specify how much of the response to return and the desired format of the returned data Handling JSON Data • Applications often request JSON data from servers. • Angular’s HttpClient simplifies fetching JSON data, with most get() calls not requiring additional options since JSON is the default data format
  • 7. OBSERVABLES IN ANGULAR What are Observables? • A method for managing asynchronous data streams and events in Angular. • They are part of the RxJS library and are used extensively within Angular applications. Key Features of Observables: • Asynchronous Operations: Handle HTTP requests, user inputs, and events. • Multiple Values Over Time: Emit zero or more values and handle them as a stream. • Cancellation: Subscriptions to observables can be canceled, unlike promises. • Operators: Apply functions to each item emitted by an observable.
  • 8. OBSERVABLES IN ANGULAR Usage in Angular • HTTP Client: Angular’s HttpClient uses observables for handling AJAX requests and responses. • EventEmitter: Components communicate with each other using EventEmitter, which is an observable that emits events. • Async Pipe: Automatically subscribes to observables and updates the view with the latest emitted values. Advantages Over Promises • More powerful and flexible, especially for complex data flows and multiple values. • Better error handling and the ability to retry operations.
  • 9. HTTP AND OBSERVABLES IN ANGULAR Synergy of HTTP and Observables: • Angular’s HttpClient uses Observables to handle HTTP requests, providing a robust framework for managing asynchronous data. Making HTTP Requests with Observables: • Use HttpClient methods like get, post, put, and delete to interact with a server. • These methods return Observables, allowing you to subscribe and handle the emitted data when the request is completed. Benefits of This Approach: • Streamlined Error Handling: Use operators like catchError to manage errors gracefully. • Cancellation: Cancel HTTP requests easily if they are no longer needed. • Retry Logic: Implement retry strategies for failed HTTP requests using the retry operator.
  • 10. FETCHING DATA WITH HTTP AND OBSERVABLES Objective: • Demonstrate how to fetch and display data from a server using Angular’s HttpClient and Observables. Service Creation: • Define a service that uses HttpClient to make a GET request. • The service returns an Observable that emits the data when the response is received. Component Subscription: • In the component, subscribe to the Observable returned by the service. • Assign the emitted data to a variable for display.
  • 11. ERROR HANDLING IN HTTP REQUESTS • Importance of Error Handling: Robust error handling is essential for a resilient Angular application, ensuring a smooth user experience even when things go wrong. • Using catchError Operator: The catchError operator is pivotal for handling errors in Observable streams, particularly in HTTP requests made with Angular’s HttpClient. • Centralizing Error Handling: Consider creating a global error-handling service that can be used across different components and services for consistency and maintainability. • Providing Meaningful Error Messages: When an error occurs, provide clear and actionable feedback to the user, without exposing sensitive details. • Logging Errors: Implement a logging strategy to capture errors for debugging and monitoring application health.