41,99 €
Discover over 70 recipes that provide the solutions you need to know to face every challenge in Angular 2 head on
This book is for developers who are competent with JavaScript and are looking to dive headfirst into the TypeScript edition of Angular 2. This book is also geared towards developers with experience in Angular 1 who are looking to make the transition.
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.
This book follows a cookbook approach—each recipe presents a unique problem to which the solution is presented in a clear, concise, and manner step-by-step manner. With practical hands-on guidance in each and every recipe, you'll be able to get to grips with the concepts.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 455
Veröffentlichungsjahr: 2017
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2017
Production reference: 1160117
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78588-192-3
www.packtpub.com
Author
Matt Frisbie
Project Coordinator
Ritika Manoj
Reviewer
Patrick Gillespie
Proofreader
Safis Editing
Acquisition Editor
Vinay Argekar
Indexer
Francy Puthiry
Content Development Editor
Arun Nadar
Graphics
Kirk D'Penha
Technical Editor
Vivek Arora
Production Coordinator
Deepika Naik
Copy Editor
Gladson Monteiro
Cover Work
Deepika Naik
Matt Frisbie is currently a software engineer at Google. He was the author of the Packt Publishing bestseller AngularJS Web Application Development Cookbook and also has published several video series through O'Reilly. He is active in the Angular community, giving presentations at meetups and doing webcasts.
Writing a book on Angular 2 while the framework itself was unfinished was an immensely challenging endeavor. Fragmented examples, incomplete documentation, and a nascent developer community were just a handful of the many roadblocks I encountered on the journey to finishing this title, and it was only because of a legion of supporters that this book was finished and was able to do justice to the framework.
This book would not have been possible without the tireless work of all the Packt staff involved. I'd specifically like to thank Arun Nadar, Vivek Arora, Merwyn D'Souza, and Vinay Argekar for their editorial oversight and expertise, as well as Patrick Gillespie for his work as content reviewer. I'd also like to thank Jordan, Zoey, Scott, and my family and friends for cheering me on.
Patrick Gillespie has been into software development since 1996. He has both a bachelor's and a master's degree in computer science. In his spare time, he enjoys photography, spending time with his family, and working on various side projects for his website (http://patorjk.com/).
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously—that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn.
You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: [email protected].
To my grandparents, Richard and Margery. Here's to upholding the family honor.
"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.
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.
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.
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.
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-devnpm install karma-cli -gNew terms and important words are shown in bold.
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
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.
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.
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:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
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!
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 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.
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.
This chapter will cover the following recipes:
The introduction of Angular 2 into the Angular ecosystem will surely be interpreted and handled differently for all developers. Some will stick to their existing Angular 1 codebases, some will start brand new Angular 2 codebases, and some will do a gradual or partial transition.
It is recommended that you become familiar with the behavior of Angular 2 components before you dive into these recipes. This will help you frame mental models as you adapt your existing applications to be more compliant with the Angular 2 style.