AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. Out of the box, it eliminates much of the code you currently write through data binding and dependency injection. And it all happens in JavaScript within the browser, making it an ideal partner with any server technology.
Angular is what HTML would have been had it been designed for applications. HTML is a great declarative language for static documents. It does not contain much in the way of creating applications, and as a result building web applications is an exercise in what do I have to do to trick the browser into doing what I want.
The impedance mismatch between dynamic applications and static documents is often solved with:
jQuery
.knockout
, ember
, etc.Angular takes another approach. It attempts to minimize the impedance mismatch between document centric HTML and what an application needs by creating new HTML constructs. Angular teaches the browser new syntax through a construct we call directives. Examples include:
{{}}
.Angular is not a single piece in the overall puzzle of building the client-side of a web application. It handles all of the DOM and AJAX glue code you once wrote by hand and puts it in a well-defined structure. This makes Angular opinionated about how a CRUD application should be built. But while it is opinionated, it also tries to make sure that its opinion is just a starting point you can easily change. Angular comes with the following out-of-the-box:
Angular simplifies application development by presenting a higher level of abstraction to the developer. Like any abstraction, it comes at a cost of flexibility. In other words not every app is a good fit for Angular. Angular was built for the CRUD application in mind. Luckily CRUD applications represent the majority of web applications. But to understand what Angular is good at one also has to understand when an app is not a good fit for Angular.
Games and GUI editors are examples of applications with intensive and tricky DOM manipulation.
These kinds of apps are different from CRUD apps, and as a result are probably not a good fit for Angular.
In these cases it may be better to use a library with a lower level of abstraction, such as jQuery
.
Below is a typical CRUD application which contains a form. The form values are validated, and are used to compute the total, which is formatted to a particular locale. These are some common concepts which the application developer may face:
Try out the Live Preview above, and then let's walk through the example and describe what's going on.
In the <html>
tag, we specify that it is an Angular
application with the ng-app
directive. The ng-app
will cause Angular to auto initialize your application.
<html ng-app>
We load Angular using the <script>
tag:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/?.?.?/angular.min.js"></script>
From the ng-model
attribute of the <input>
tags, Angular automatically sets up two-way data
binding, and we also demonstrate some easy input validation:
Quantity: <input type="number" ng-pattern="/\d+/" step="1" min="0" ng-model="qty" required >
Cost: <input type="number" ng-model="cost" required >
These input widgets look normal enough, but consider these points:
qty
and cost
) to
variables of the same name. Think of those variables as the "Model" component of the
Model-View-Controller design pattern.input
has special powers. The input invalidates itself by turning red when you enter invalid data or
leave the input fields blank. These new widget behaviors make it easier to implement field
validation common in CRUD applications.And finally, the mysterious {{ double curly braces }}
:
Total: {{qty * cost | currency}}
This notation, {{ _expression_ }}
, is Angular markup for data-binding. The expression itself can
be a combination of both an expression and a filter: {{
expression | filter }}
. Angular provides filters for formatting display data.
In the example above, the expression in double-curly braces directs Angular to "bind the data we got from the input widgets to the display, multiply them together, and format the resulting number into output that looks like money."
Notice that we achieved this application behavior not by calling Angular methods, nor by implementing application specific behavior as a framework. We achieved the behavior because the browser behaved more in line with what is needed for a dynamic web application rather then what is needed for a static document. Angular has lowered the impedance mismatch to the point where no library/framework calls are needed.
Angular is built around the belief that declarative code is better than imperative when it comes to building UIs and wiring software components together, while imperative code is excellent for expressing business logic.
Angular frees you from the following pains:
Here is a presentation on Angular from May 2012. The corresponding slides are also available.