@@ -9,117 +9,118 @@ block includes
9
9
.l-sub-section
10
10
:marked
11
11
The [`WebSocket`](https://tools.ietf.org/html/rfc6455) protocol is another important communication technology;
12
- we won 't cover it in this chapter .
12
+ it isn 't covered in this page .
13
13
:marked
14
14
Modern browsers support two HTTP-based APIs:
15
15
[XMLHttpRequest (XHR)](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) and
16
16
[JSONP](https://en.wikipedia.org/wiki/JSONP). A few browsers also support
17
17
[Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
18
18
19
- The !{_Angular_http_library} simplifies application programming of the **XHR** and **JSONP** APIs
20
- as we'll learn in this chapter covering :
19
+ The !{_Angular_http_library} simplifies application programming with the **XHR** and **JSONP** APIs.
20
+ This page covers :
21
21
22
- - [HTTP client sample overview ](#http-client)
22
+ - [The Tour of Heroes * HTTP* client demo ](#http-client)
23
23
- [Fetch data with http.get](#fetch-data)
24
- <li if-docs="ts"> [RxJS Observable of HTTP Responses ](#rxjs)</li>
25
- <li if-docs="ts"> [Enabling RxJS Operators ](#enable-rxjs-operators)</li>
26
- - [Extract JSON data ](#extract-data)
27
- - [Error handling ](#error-handling)
24
+ <li if-docs="ts"> [RxJS library ](#rxjs)</li>
25
+ <li if-docs="ts"> [Enable RxJS operators ](#enable-rxjs-operators)</li>
26
+ - [Process the response object ](#extract-data)
27
+ - [Always handle errors ](#error-handling)
28
28
- [Send data to the server](#update)
29
- <li if-docs="ts"> [Promises instead of observables ](#promises)</li>
29
+ <li if-docs="ts"> [Fall back to promises ](#promises)</li>
30
30
- [Cross-origin requests: Wikipedia example](#cors)
31
31
<ul if-docs="ts">
32
- <li> [Set query string parameters](#search-parameters)</li>
33
- <li> [Debounce search term input ](#more-observables)</li>
32
+ <li> [Search parameters](#search-parameters)</li>
33
+ <li> [More fun with observables ](#more-observables)</li>
34
34
</ul>
35
- - [Appendix: the in-memory web api service ](#in-mem-web-api)
35
+ - [Appendix: Tour of Heroes in-memory server ](#in-mem-web-api)
36
36
37
- We illustrate these topics with code that you can <live-example>run live</live-example>.
37
+ These topics are illustrated with code that you can <live-example>run live</live-example>.
38
38
39
39
.l-main-section
40
40
:marked
41
41
# Demos
42
42
43
- This chapter describes server communication with the help of the following demos
43
+ This page describes server communication with the help of the following demos:
44
44
45
45
block demos-list
46
46
:marked
47
- - [HTTP client: Tour of Heroes with Observables ](#http-client)
48
- - [HTTP client: Tour of Heroes with !{_Promise}s](#promises)
49
- - [JSONP client : Wikipedia to fetch data from a service that does not support CORS ](#cors)
50
- - [JSONP client: Wikipedia using observable operators to reduce server calls ](#more-observables)
47
+ - [The Tour of Heroes *HTTP* client demo ](#http-client)
48
+ - [Fall back to !{_Promise}s](#promises)
49
+ - [Cross-origin requests : Wikipedia example ](#cors)
50
+ - [More fun with observables ](#more-observables)
51
51
52
52
:marked
53
- These demos are orchestrated by the root `AppComponent`
53
+ The root `AppComponent` orchestrates these demos.
54
54
+ makeExample('server-communication/ts/app/app.component.ts' , null , 'app/app.component.ts' )
55
55
56
56
+ ifDocsFor('ts' )
57
57
:marked
58
- There is nothing remarkable here _except_ for the import of RxJS operators.
58
+ There is nothing remarkable here _except_ for the import of RxJS operators, which is
59
+ described [later](#rxjs).
59
60
+ makeExample('server-communication/ts/app/app.component.ts' , 'import-rxjs' )( format ='.' )
60
- :marked
61
- We'll talk about that [below](#rxjs) when we're ready to explore observables.
62
61
:marked
63
- First, we have to configure our application to use server communication facilities.
62
+ First, configure the application to use server communication facilities.
64
63
65
64
.l-main-section #http-providers
66
65
:marked
67
- # Providing HTTP Services
66
+ # Providing HTTP services
68
67
69
- We use the !{_Angular_Http} client to communicate with a server using a familiar HTTP request/response protocol.
68
+ The !{_Angular_Http} client communicates with the server using a familiar HTTP request/response protocol.
70
69
The `!{_Http}` client is one of a family of services in the !{_Angular_http_library}.
71
70
72
71
+ ifDocsFor('ts' )
73
72
.l-sub-section
74
73
:marked
75
- SystemJS knows how to load services from the !{_Angular_http_library} when we import from the `@angular/http` module
76
- because we registered that module name in the `system.config` file.
74
+ When you import from the `@angular/http` module, SystemJS knows how to load services from
75
+ the !{_Angular_http_library}
76
+ because you registered that module name in the `system.config` file.
77
77
78
78
:marked
79
- Before we can use the `!{_Http}` client , we'll have to register it as a service provider with the Dependency Injection system.
79
+ Before you can use the `!{_Http}` client, you need to register it as a service provider with the dependency injection system.
80
80
81
81
.l-sub-section
82
82
:marked
83
- Learn about providers in the [Dependency Injection](dependency-injection.html) chapter .
83
+ Read about providers in the [Dependency Injection](dependency-injection.html) page .
84
84
85
85
:marked
86
- In this demo, we register providers by importing other NgModules to our root NgModule.
86
+ Register providers by importing other NgModules to the root NgModule.
87
87
88
88
+ makeExample('server-communication/ts/app/app.module.1.ts' , null , 'app/app.module.ts (v1)' )( format ='.' )
89
89
90
90
block http-providers
91
91
:marked
92
- We begin by importing the symbols we need, most of them familiar by now.
92
+ Begin by importing the necessary symbols; most of them should be familiar by now.
93
93
The newcomers are the `HttpModule` and the `JsonpModule` from the !{_Angular_http_library}.
94
94
95
- We add these modules to the application by passing them to the `imports` array in our root NgModule.
95
+ To add these modules to the application, pass them to the `imports` array in the root NgModule.
96
96
.l-sub-section
97
97
:marked
98
- We need the HttpModule to make HTTP calls.
99
- We don't need the JsonpModule for plain HTTP.
100
- We will demonstrate JSONP support later in this chapter .
101
- We're loading its module now to save time.
98
+ You need the HttpModule to make HTTP calls.
99
+ Though the JsonpModule isn't necessary for plain HTTP,
100
+ there is a JSONP demo later in this page .
101
+ Loading its module now saves time.
102
102
.l-main-section #http-client
103
103
:marked
104
- # The Tour of Heroes _HTTP_ Client Demo
104
+ # The Tour of Heroes _HTTP_ client demo
105
105
106
- Our first demo is a mini-version of the [tutorial](../tutorial)'s "Tour of Heroes" (ToH) application.
107
- This version gets some heroes from the server, displays them in a list, lets us add new heroes, and saves them to the server.
108
- We use the !{_Angular_Http} client to communicate via `XMLHttpRequest (XHR)`.
106
+ The first demo is a mini-version of the [tutorial](../tutorial)'s "Tour of Heroes" (ToH) application.
107
+ This version gets some heroes from the server, displays them in a list, lets you add new heroes, and saves them to the server.
108
+ You use the !{_Angular_Http} client to communicate via `XMLHttpRequest (XHR)`.
109
109
110
- It works like this.
110
+ It works like this:
111
111
figure.image-display
112
112
img( src ='/resources/images/devguide/server-communication/http-toh.gif' alt ="ToH mini app" width ="250" )
113
113
:marked
114
114
This demo has a single component, the `HeroListComponent`. Here's its template:
115
115
+ makeExample('server-communication/ts/app/toh/hero-list.component.html' , null , 'app/toh/hero-list.component.html (template)' )
116
116
:marked
117
117
It presents the list of heroes with an `ngFor`.
118
- Below the list is an input box and an *Add Hero* button where we can enter the names of new heroes
118
+ Below the list is an input box and an *Add Hero* button where you can enter the names of new heroes
119
119
and add them to the database.
120
- We use a [template reference variable](template-syntax.html#ref-vars), `newHeroName`, to access the
120
+ A [template reference variable](template-syntax.html#ref-vars), `newHeroName`, discussed in the
121
+ [Template Syntax](template-syntax.html) page, accesses the
121
122
value of the input box in the `(click)` event binding.
122
- When the user clicks the button, we pass that value to the component's `addHero` method and then
123
+ When the user clicks the button, you pass that value to the component's `addHero` method and then
123
124
clear it to make it ready for a new hero name.
124
125
125
126
Below the button is an area for an error message.
@@ -167,7 +168,7 @@ block getheroes-and-addhero
167
168
a#HeroService
168
169
.l-main-section #fetch-data
169
170
:marked
170
- ## Fetch data with the **HeroService**
171
+ ## Fetch data with http.get
171
172
172
173
In many of our previous samples we faked the interaction with the server by
173
174
returning mock heroes in a service like this one:
0 commit comments