Book Image

Angular 2 By Example

By : Chandermani Arora, Kevin Hennessy
Book Image

Angular 2 By Example

By: Chandermani Arora, Kevin Hennessy

Overview of this book

<p>Angular 2 will help you build faster, more efficient, and more flexible cross-platform applications. Angular 2 is known for taking the pain out of JavaScript development, and enabling more organized, readable, and testable code.</p> <p>This book builds three apps with varying degrees of complexity. It starts with a simple ‘Guess the Number’ game, which serves as a platform to launch you into the world of Angular. Next, you will learn to construct a popular ‘7-Minute Workout’ app, covering the building blocks of Angular. The final app, ‘Personal Trainer’ morphs the existing ‘7-Minute Workout’ into a full-fledged personal workout builder and runner, covering advanced directive building, which is the most fundamental and powerful feature of Angular.</p> <p>In addition to this, you will learn about testability and the framework constructs Angular provides to effectively test your app. The book concludes by providing you with practical advice and useful tips that will come in handy as you build more and more apps with Angular.</p>
Table of Contents (14 chapters)
Angular 2 By Example
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface

The async pipe


As we have seen with many of the data operations covered in this chapter, there is a fairly common pattern being repeated over and over again. When an Observable is returned from an HTTP request, we convert the response to JSON and subscribe to it. The subscription then binds the Observable output to a UI element. Wouldn't it be nice if we could eliminate this repetitive coding and replace it with a simpler way to accomplish what we are wanting to do?

Not surprisingly, Angular provides us with just the right way to do that. It's called the async pipe, and it can be used like any other pipe for binding to an element on the screen. However, the async pipe is a much more powerful mechanism than other pipes. It takes an Observable or a promise as an input and subscribes to it automatically. It also handles the tear down of the subscription for an Observable without necessitating any further lines of code.

Let's look at an example of this in our application. Let's go back to the...