INTRODUCTION
Its an exciting time to be a JavaScript developer. Between the meteoric rise of server-side JavaScripts open source community (100,000 packages on the NodeJS package manager as of October 2014twice as many as in December 2013), the popularity of next-generation client-side frameworks like AngularJS, and the growing number of companies that build web tools based on full-stack JavaScript, JavaScript language skills are in high demand. Modern tools allow you to build sophisticated browser-based clients, highly concurrent servers, and even hybrid native mobile applications using a single language. AngularJS is quickly becoming the leading next-generation client-side web framework, enabling individuals, small teams, and large corporations to build and test phenomenally sophisticated browser-based applications.
WHAT IS ANGULARJS?
Within the rapidly growing JavaScript community, AngularJS burst onto the scene when it released version 1.0 in June 2012. Although a relatively new framework, its powerful features and elegant tools for structuring applications have made it the front-end framework of choice for many developers. AngularJS was originally developed at Google by testing engineer Misko Hevery, who found that existing tools, like jQuery, made it difficult to structure browser user interfaces (UIs) that needed to display large amounts of sophisticated data. Google now has a dedicated team developing and maintaining AngularJS and related tools. AngularJS also powers some active Google applications, ranging from the DoubleClick Digital Marketing Platform to the YouTube app on the PlayStation 3. AngularJSs popularity is growing rapidly: As of October 2014, it powers 143 of the Quantcast Top 10k websites and is rapidly outpacing its closest rivals, KnockoutJS, ReactJS, and EmberJS.
What makes AngularJS so special? One particularly pithy expression borrowed from the https://angularjs.org/
website describes AngularJS as enabling you to write less code, go have beer sooner. The heart of AngularJS is a concept called two-way data binding, which enables you to bind Hypertext Markup Language (HTML) and cascading style sheets (CSS) to the state of a JavaScript variable. Whenever the variable changes, AngularJS updates all HTML and CSS that references that JavaScript variable. For instance, in the following code:
ng-show="shouldShow">Hello
If the shouldShow
variable is changed to false
, AngularJS automatically hides the div
element for you. There is nothing special about the shouldShow
variable: AngularJS doesnt require you to wrap your variables in special types; the shouldShow
variable can be a plain old JavaScript Boolean value.
Although two-way data binding is the basis for what makes AngularJS so useful, its only the tip of the iceberg. AngularJS provides an elegant framework for organizing your client-side JavaScript in a way to maximize reusability and testability. In addition, AngularJS has a rich set of testing tools, such as Karma, protractor, and ngScenario (see Chapter 9), which are optimized for use with AngularJS. AngularJSs focus on testable structures and rich testing tools makes it a natural choice for mission-critical client-side JavaScript. Not only does it enable you to write sophisticated applications fast, it supplies tools and structure that make testing your application easy. As a matter of fact, Googles DoubleClick team cited AngularJSs full testing story as one of its six biggest reasons for porting its digital marketing platform to AngularJS. Here is a brief overview of some of the concepts that make AngularJS special.
Two-Way Data Binding
In many older client-side JavaScript libraries, like jQuery and Backbone, you are expected to manipulate the Document Object Model (DOM) yourself. In other words, if you want to change the HTML contents of a div
element, you need to write imperative JavaScript. For example:
$('div').html('Hello, world!');
AngularJS inverts this paradigm and makes your HTML the definitive source for how your data is displayed. The primary purpose of two-way data binding is to bind an HTML or CSS property (for instance, the HTML contents or background color of a div
element) to the value of a JavaScript variable. When the value of the JavaScript variable changes, the HTML or CSS property is updated to match. The opposite is also true: If the user types in an input
field, the value of the bound JavaScript variable is updated to match what the user typed. For instance, the following HTML greets whoevers name is typed in the input field. You can find this example in this chapters sample code as data _ binding.html
: Simply right-click on the file and open it in your browserno web server or other dependencies required!
ng-model="user" placeholder="Your Name">
Hello, {{user}}!
No JavaScript is necessary! The ngModel
directive and the {{}}
shorthand syntax do all the work. There is limited benefit to using AngularJS in this simple example, but, as youll see when you build a real application in Chapter 1, data binding greatly simplifies your JavaScript. Its not uncommon to see 800 lines of jQuery spaghetti code reduced to 40 lines of clean DOM-independent AngularJS code thanks to data binding.
Scopes in the DOM
DOM scopes are another powerful feature of AngularJS. As you might have guessed, there is no free lunch with data binding; code complexity has to go somewhere. However, AngularJS allows you to create scopes in the DOM that behave similarly to scopes in JavaScript and other programming languages. This permits you to break your HTML and JavaScript into independent and reusable pieces. For instance, heres the same greeting example from earlier, but with two separate scopes: one for greeting in English, the other in Spanish:
ng-controller="HelloController">
Hello, {{user}}!
ng-controller="HelloController">
Hola, {{user}}!
function HelloController($scope) {}
The ngController
directive is one way to create a new scope, enabling you to reuse the same code for two different purposes. Chapter 4 includes a thorough overview of two-way data binding and a discussion of internal implementation details.
Directives
Directives are a powerful tool for grouping HTML and JavaScript functionality into one easily reusable bundle. AngularJS has numerous built-in directives, like the ngController
and ngModel
directives you saw earlier, that enable you to access sophisticated JavaScript functionality from your HTML. You can write your own custom directives as well. In particular, AngularJS allows you to associate HTML with a directive, so you can use directives as a way of reusing HTML as well as a way of tying certain behavior into two-way data binding. Writing custom directives is beyond the scope of this introduction, but Chapter 5 includes a thorough discussion of the subject.
Templates
On top of two-way data binding, AngularJS lets you swap out entire portions of the page based on the state of a JavaScript variable. The ngInclude
directive enables you to conditionally include templates, pieces of AngularJS-infused HTML, in the page based on the JavaScript state. The following example demonstrates a page with a
Next page