Book Image

AngularJS Essentials

By : Rodrigo Branas
Book Image

AngularJS Essentials

By: Rodrigo Branas

Overview of this book

<p>This book is a practical guide filled with real-world examples that will help you discover the best practices of the AngularJS framework, covering its most important concepts such as directives, expressions, filters, and modules and guiding you through the steps of building your very own web application.</p> <p>You will start by learning how to create reusable components with directives, experiencing an expressive way of developing software. Then we will move on to cover data handling, and you will learn how to use the various features of the AngularJS framework to accomplish any challenge related to presenting, transforming, and validating data on a user's interface.</p> <p>After that, we will explore the secrets of the dependency injection mechanism available in AngularJS, bringing reuse and testability to your application by decoupling the layers. The book will also cover the best practices of using the framework and how to automate the test and the project's workflow.</p>
Table of Contents (16 chapters)

Preface

For more than 12 years, I have been developing all kinds of web applications, and along the way, I have had the opportunity to experience the vast majority of frameworks on the Java platform. In 2008, I moved from an architecture highly based on backend web frameworks such as Struts and JSF to experience new challenges at the frontend. I think the main goal was to stop creating those old-school and hard-to-use web applications, investing on interactivity and usability.

At that time, I adopted the Google Web Toolkit, also known as GWT, building some web applications for almost 2 years. The results were pretty amazing in terms of user experience; however, I felt very upset about low productivity and also the amount of code that I had to write every day.

After that, in 2010, I decided to change drastically, adopting a much simpler approach by using just HTML, CSS, and JavaScript to write the frontend code. The experience was fantastic, which provided me with a very fast feedback cycle. The only problem was the lack of a layered architecture, which was unable to provide a clear separation of concerns while working with the JavaScript language. Also, I was missing things such as a strong dependency injection mechanism that would allow me to create reusable and testable components.

While looking for a solution, a very experienced JavaScript developer and also a great friend of mine, Rafael Nami, introduced me to AngularJS. In the following weeks, I started to read everything about it and also writing some code. After a few weeks, I was thrilled because it had never been so easy to create amazing web applications with so little code!

Only 2 months later, I launched my first web application based entirely on AngularJS, and honestly, I cannot imagine writing this same application using another kind of technology in this short period of time. I was so excited about it that I wrote an article on using AngularJS with Spring MVC and Hibernate for a magazine called Java Magazine. After that, I created an AngularJS training program that already has more than 200 developers who enrolled last year.

This book, AngularJS Essentials, is the result of that experience. This is a very practical guide, filled with many step-by-step examples that will lead you through the best practices of this amazing framework.

We are going to start, after a brief introduction, by learning how to create reusable components with directives. Then, we will take a look at many data handling techniques, discovering a complete set of technologies that are capable to accomplish any challenge related to present, transform, and validate data on the user's interface.

After that, we will explore the secrets of the dependency injection mechanism and also learn how to create services in order to improve the application's design. Also, we are going to discover the best way to deal with the scope and how to break up the application into separate modules, giving rise to reusable and interchangeable libraries.

Finally, we are going to learn how to test each component of the framework using Jasmine and also how to automate the workflow, creating an optimized distribution package with Grunt.

Rodrigo Branas

Software Architect, Author and International Speaker

Agile Code

What this book covers

Chapter 1, Getting Started with AngularJS, introduces the framework and its architectural model. After that, we will start coding our first application and also understand how to organize our project.

Chapter 2, Creating Reusable Components with Directives, explains how the directives are one of the most important features of the framework. With them, we will understand how to extend the HTML language vocabulary, creating new behaviors and reusable components.

Chapter 3, Data Handling, explains how the framework provides a complete set of technologies to fulfill any requirement about presenting, transforming, synchronizing, and validating data on the user's interface. We will go through all of these technologies in order to improve the user experience with our applications.

Chapter 4, Dependency Injection and Services, explains how we are going to create reusable and decoupled components by implementing services and using the dependency injection mechanism.

Chapter 5, Scope, discusses how scope is one of the main concepts of the framework. In this chapter, we will discover the best practices to deal with scope.

Chapter 6, Modules, briefs us on how the framework is strongly based on the modules. In this chapter, we will understand how to break up our application into modules.

Chapter 7, Unit Testing, shows how we will dive deeply into testing techniques. We are going to understand how to test each framework component using Jasmine.

Chapter 8, Automating the Workflow, discusses how we will create an optimized distribution package for our application using Grunt and its plugins. Also, we will discover how to manage our dependencies with Bower.

What you need for this book

To implement the code in this book, you will need to use your favorite development interface and a web browser. I would recommend sublime text, but you may use Aptana (which is based on Eclipse), WebStorm, or any other IDE.

AngularJS is compatible with the most browsers such as Firefox, Chrome, Safari, and Internet Explorer. Feel free to choose the one you are used to.

Who this book is for

If you have a passion for web development and are looking for a framework that could provide a reusable, maintainable, and modular way to create applications, and at the same time, help increase your productivity and satisfaction, this is the book for you.

Conventions

In this book, you will find a number of styles of text 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: "The $http service wraps the low-level interaction with the XMLHttpRequest object, providing an easy way to perform AJAX calls without headaches."

A block of code is set as follows:

$http.get("/cars")
  .success(function(data, status, headers, config) {
    $scope.car = data;
  })
  .error(function(data, status, headers, config) {
    console.log(data);
  });

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

$http.get("/cars")
  .success(function(data, status, headers, config) {
    $scope.car = data;
  })
  .error(function(data, status, headers, config) {
    console.log(data);
  });

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

bower install angular

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "What happens when we change the plate and click on the Show Plate button?".

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on 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 all Packt books you have purchased 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.

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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.