Book Image

Angular 2 Cookbook

By : Patrick Gillespie, Matthew Frisbie
Book Image

Angular 2 Cookbook

By: Patrick Gillespie, Matthew Frisbie

Overview of this book

Angular 2 introduces an entirely new way to build applications. It wholly embraces all the newest concepts that are built into the next generation of browsers, and it cuts away all the fat and bloat from Angular 1. This book plunges directly into the heart of all the most important Angular 2 concepts for you to conquer. In addition to covering all the Angular 2 fundamentals, such as components, forms, and services, it demonstrates how the framework embraces a range of new web technologies such as ES6 and TypeScript syntax, Promises, Observables, and Web Workers, among many others. This book covers all the most complicated Angular concepts and at the same time introduces the best practices with which to wield these powerful tools. It also covers in detail all the concepts you'll need to get you building applications faster. Oft-neglected topics such as testing and performance optimization are widely covered as well. A developer that reads through all the content in this book will have a broad and deep understanding of all the major topics in the Angular 2 universe.
Table of Contents (18 chapters)
Angular 2 Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface

Preface

"Everybody has a plan until they get punched in the mouth."                                                                                                      -Mike Tyson, undisputed heavyweight champion boxer

Soon after its creation in 2009, AngularJS grew into a widely popular foundational tool for building frontend applications. As years and releases went by, and the JavaScript community matured, the world of client-side programming broadened beyond what Angular was originally designed for. Its caretakers took stock and decided that a sweeping overhaul of the framework was in order.

AngularJS, now Angular 1, still exists and will be supported for the years to come, but in its wake lies Angular 2—a wholly different animal built for the future of client-side computing. Angular 2 abandons antipatterns by the fistful and, instead, is reshaped into a precise and elegant software instrument. It embraces the impending renaissance of web technologies, building atop ES6, web components, web workers, TypeScript, and reactive programming, to name a few. It brings framework modularity to new heights, building itself around the concept that any modular piece of Angular 2 should be easily discarded or replaced. Best of all, Angular 2 offers a bountiful collection of configuration and tooling that will make your applications run at breakneck speed.

To many developers, Angular 2 is frightening because so much of it is new and unfamiliar. This book exists to offer you an approachable path to a full understanding of Angular 2, what it offers, and how best to use it. You will find both simple examples to set a foundational understanding, and complex demonstrations to hint at the framework's power. The book is organized into recipes that are independent of each other, so you are able to jump in at any point and immediately begin learning.

What this book covers

This book is up to date for the 2.4 release and is compatible through the 4.0 release as well, and it does not have any code based on the beta or release candidates.

Chapter 1, Strategies for Upgrading to Angular 2, is an overview of a number of ways to migrate an Angular 1 application to Angular 2. Although there is no one-size-fits-all upgrade strategy, you will find that these recipes demonstrate some ways that will allow you to preserve a large amount of your existing Angular 1 code base.

Chapter 2, Conquering Components and Directives, gives a broad and deep set of examples involving what Angular 2 components are and how to use them. Angular 2 applications are built entirely of components, and this chapter offers you a total rundown of their role.

Chapter 3, Building Template-Driven and Reactive Forms, covers the reworked Angular 2 form modules. Angular 2 offers you two primary styles of erecting form features, and this chapter covers both of them in depth.

Chapter 4, Mastering Promises, shows how the Promise object has a role in Angular 2. Although RxJS has subsumed some of the usefulness of Promises, they are still first-class citizens in ES6 and still play a crucial role.

Chapter 5, ReactiveX Observables, gives you a crash course in how Angular 2 has embraced reactive programming. It includes recipes that demonstrate the basics of Observables and Subjects, as well as advanced implementations that take RxJS to its limits.

Chapter 6, The Component Router, takes you through the totally reworked routing module in Angular 2. It covers both routing basics as well as an array of advanced routing concepts unique to Angular 2.

Chapter 7, Services, Dependency Injection, and NgModule, describes the new and improved dependency injection and module strategies of Angular 2. It gives you all the pieces you need to break your application into independent services and modules, as well as ideal strategies for connecting those pieces together.

Chapter 8, Application Organization and Management, is a broad overview of how you can manage your Angular 2 application inside and outside the client. Angular 2 introduces a number of layers of complexity that require advanced tooling, and this chapter will guide you through how to approach them.

Chapter 9, Angular 2 Testing, will guide you through both how to set up test suites for Angular 2 as well as how to write various types of tests for these suites. Many developers avoid testing when learning a framework anew, and this chapter gently guides you through Angular 2's excellent test infrastructure.

Chapter 10, Performance and Advanced Concepts, is a crash course on the dizzying array of complex concepts that Angular 2 comes with out of the box. This chapter covers program organization and architecture, framework features and tooling, as well as compile-time optimizations.

What you need for this book

Every recipe in this book is accompanied by a link to the book's companion site, http://ngcookbook.herokuapp.com/. Recipes that involve code examples will include a link to a live example on Plunker. This will allow you to inspect and test code in real time without having to worry about compilation, local servers, or anything of that ilk. It must be noted, however, that this setup is only appropriate for experimentation and should not be used for user-facing or production applications.

Angular 2 comes in both JavaScript and TypeScript flavors, but this book aims directly at the TypeScript edition, since it is syntactically superior (as you will soon realize). For proper production applications, TypeScript will be compiled into JavaScript before it is served to the browser. The way this book accomplishes this (and many other code preparation tasks) is inside a Node.js install on your local machine. Node.js includes the Node Package Manager (npm), which lets you install and run open source JavaScript software from the command line.

Some chapters in this book will require that you have Node.js installed before running commands and launching a local server or test suite. Furthermore, it is recommended (but not required) that you install the Node Version Manager on top of Node.js, which will make managing your installed packages much easier.

Who this book is for

The universe of Angular 2 learning materials is currently fragmented and gross. This book is for both beginner developers looking to sink their teeth into a new framework, as well as advanced developers interested in rounding out their knowledge of a framework that embraces the coming world of web tech.

For newer developers, ingesting all these new technologies at once may seem overwhelming. The organization and pace of this book is designed so that topics are gradually introduced, and design decisions and rationales are explained. Don't worry, this book is still for you.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Karma reads its configuration out of a karma.conf.js file."

A block of code is set as follows:

<p>{{date}}</p> 
<h1>{{title}}</h1> 
<h3>Written by: {{author}}</h3>

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

@Component({ 
  selector: 'article', 
  template: ` 
    <p>{{currentDate|date}}</p> 
    <h1>{{title}}</h1> 
    <h3>Written by: {{author}}</h3> 
  ` 
})

Any command-line input or output is written as follows:

npm install karma jasmine-core karma-jasmine --save-dev
npm install karma-cli -g

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Angular-2-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.