Module 5
Module 5
Chained pipes
We can combine multiple pipes together. This
will be useful when a scenario associates with
more than one pipe that has to be applied for
data transformation. In the above example, if
Adding the parameters you want to show the date with uppercase
letters, then we can apply
Create a date method in both Date and Uppercase pipes together.
your app.component.ts file.
<div> AsyncPipe
Today's date :- {{presentDate}} If data comes in the form of observables,
</div> then Async pipe subscribes to an observable
and returns the transmitted values.
Now, run the application, it will show the
import { Observable } from 'rxjs';
following output −
const observer = {
numbers$ is an Observable object, next: (num: number) =>
which when subscribed will emit 1 to { this.numbers.push(num); this.val1 +=
10 in a sequence. num },
Dollar sign ($) at the end of the
error: (err: any) => console.log(err),
variable is to identify that the
variable is Observable. complete: () =>
console.log("Observation completed")
2. range − Emit a range of number in sequence.
};
3. from − Emit array, promise or iterable. next − method get the emitted
number and then push it into the
const numbers$ = local variable, this.numbers.
from([1,2,3,4,5,6,7,8,9,10]); next − method also adding the
number to local variable, this.val1.
4. ajax − Fetch a url through AJAX and then error − method just writes the error
emit the response. message to console.
complete − method also writes the
const api$ = ajax({ url: completion message to console.
'https://httpbin.org/delay/1', method:
'POST', headers: { 'Content-Type': We can skip error and complete method and
'application/text' }, body: "Hello" }); write only the next method as shown below −
const clickEvent$ =
fromEvent(document.getElementById('counter'), Operations
'click');
Rxjs library provides some of the operators to
Angular internally uses the concept extensively process the data stream. Some of the
to provide data transfer between components important operators are as follows −
and for reactive forms. filter − Enable to filter the data stream using
callback function.
Subscribing process
const filterFn = filter( (num : number) =>
Subscribing to an Observable is quite easy. num > 5 );
Every Observable object will have a method, const filteredNumbers$ = filterFn(numbers$);
subscribe for the subscription process.
Observer need to implement three callback
filteredNumbers$.subscribe( (num : number) =>
{
this.filteredNumbers.push(num); this.val2 +=
num } );
Let us create a sample application to try out the REST is a hybrid style derived from several of
reaction programming concept learned in this the network-based architectural styles and
chapter. combined with additional constraints that
define a uniform connector interface. The
Create a new application, reactive using below software architecture framework is used to
command − define the architectural elements of REST and
examine sample process, connector, and data
ng new reactivedemo views of prototypical architectures.
Change the directory to the newly created Sharing data between two or more systems has
application. always been a fundamental requirement of
software development. For example, consider
cd reactive buying motor insurance. Your insurer must
obtain information about you and your vehicle
Create a new component called 'reactive' so they request data from car registration
authorities, credit agencies, banks, and other
Open the reactive.component.ts and code the systems. All this happens transparently in real-
following: time to determine whether a policy can be
offered.
Deriving REST
The rationale behind the Web architecture can
be described by an architectural
style consisting of the set
of constraints applied to elements within the
architecture. By examining the impact of each
constraint as it is added to the evolving style,
we can identify the properties induced by the
Web's constraints. Additional constraints can
then be applied to form a new architectural
style that better reflects the desired properties
of a modern Web architecture.
This section provides a brief overview of REST Figure 1. Resources list found on JSON
by walking through the process of deriving it as Placeholder Website
an architectural style. Later sections will
describe in more detail the specific constraints
that compose the REST style.
Figure 3. Importing the new components and
services created.
Figure 5. Final output.