AngularJS is created for those developers who believe that the declarative style is better suited for creating a UI, and imperative - for writing business logic.
Zen Angular
- It is good to separate the DOM th manipulation from the application logic. This significantly improves the testability of the code.
- It's good to assume that automated testing of an application is as important as the writing of the application itself. Testability is highly dependent on how the code is structured.
- It is good to separate the development of the client from the server. This allows you to conduct development in parallel and improves reuse on both sides.
- It is good when the framework leads the developer throughout the application development cycle: from designing the UI through writing business logic to testing.
- Well, when common tasks become trivial, and complex - are simplified.
AngularJS is a comprehensive framework. In the standard package it provides the following options:
- All you need to create CRUD applications: data-binding, basic directives for templates, form validation, routing, deep linking, component reuse, dependency injection, tools for interacting with server (RESTful) data sources.
- All you need for testing: unit testing tools, end-to-end testing, mock-and.
- Template of a typical application that includes directory structure and test scripts.
')
AngularJS is developed by Google employees and is used in at least one Google service -
DoubleClick .
Examples
Simple todo-shka
<div ng-app> <h2>Todo</h2> <div ng-controller="TodoCtrl"> <span>{{remaining()}} of {{todos.length}} remaining</span> [ <a href="" ng-click="archive()">archive</a> ] <ul class="unstyled"> <li ng-repeat="todo in todos"> <input type="checkbox" ng-model="todo.done"> <span class="done-{{todo.done}}">{{todo.text}}</span> </li> </ul> <form ng-submit="addTodo()"> <input type="text" ng-model="todoText" size="30" placeholder="add new todo here"> <input class="btn-primary" type="submit" value="add"> </form> </div> </div> ​
function TodoCtrl($scope) { $scope.todos = [ {text:'learn angular', done:true}, {text:'build an angular app', done:false}]; $scope.addTodo = function() { $scope.todos.push({text:$scope.todoText, done:false}); $scope.todoText = ''; }; $scope.remaining = function() { var count = 0; angular.forEach($scope.todos, function(todo) { count += todo.done ? 0 : 1; }); return count; }; $scope.archive = function() { var oldTodos = $scope.todos; $scope.todos = []; angular.forEach(oldTodos, function(todo) { if (!todo.done) $scope.todos.push(todo); }); }; }
In action, you can see on the main page of
angularjs.org . There are also a number of examples:
- "The Basics" is a simple illustration of databinding.
- "Add Some Control" is a todo-box, the code of which I brought here.
- Wire up a Backend is a simple application for creating / storing / editing records with routing and data storage in mongolab .
- "Create Components" - the creation of reusable components.
More examples:
- todo ( demo , code ) from todomvc - at the same time you can equalize with other frameworks;
- builtwith.angularjs.org : 18 (at the time of writing the post) applications, for most of which source code is available;
- AngularUI - different filters and directives (mostly UI-shnye) from third-party developers.
AngularJS concepts
Directives
Practically the entire declarative part of AngularJS rests on directives. They are used to enrich the HTML syntax. During the compilation of DOM directives are taken from HTML and executed. Directives can add some new behavior and / or modify the DOM. The standard package includes a sufficiently large number of directives for building web applications. But the key feature is the ability to develop its own directives, due to which HTML can be turned into DSL.
Directives are named using lowerCamelCase, for example, ngBind. When using a directive, you must name it in lower case using one of the special characters as a delimiter
:
,
-
, or
_
. Optionally, to obtain a valid code, you can use the prefixes
x-
or
data-
. Examples:
ng:bind
,
ng-bind
,
ng_bind
,
x-ng-bind
and
data-ng-bind
.
Directives can be used as an element (
<my-directive></my-directive>
), attribute (), in class () or in comment (). It depends on how the specific directive was developed.
Read more about directives in the developer’s guide .
Scopes
Scope is an object related to a model in an application. It is the execution context for expressions. Scope s are arranged in a hierarchical structure similar to the DOM. At the same time, they inherit properties from their parent scope.
Scope s are like a “glue” between the controller and the view. During the execution of the template binding phase, the directives set an observation (
$watch
) for the expressions within the scope.
$watch
gives directives the ability to react to changes to display the updated value or some other action. Both controllers and directives have a reference to scope, but no reference to each other. So controllers are isolated from directives and from DOM. Due to this, the possibilities for testing the application increase.
Learn more about scope in the developer’s guide .
Services
Services are singletones that perform a specific task that is common to all or a particular web application. For example, a
$ http service that wraps XMLHttpRequest. Some examples of other services (see the complete list in the
documentation ):
- $ compile - compiling an HTML string or part of a DOM into a template, associating a template with a specific scope;
- $ cookies - provides read / write access to cookies.
- $ location - work with the address bar
- $ resource - a factory for creating resource objects for interacting with server (RESTful) data sources;
To use the service, you must specify it as a dependency for the controller, another service, directive, etc. AngularJS takes care of everything else - creating, resolving dependencies, etc.
Read more about the services in the developer’s guide .
Filters
Filters are designed to format data before displaying it to the user, as well as filter items in collections. Examples of filters (a complete list can be found in the
documentation ):
currency ,
date ,
orderBy ,
uppercase . Using filters is quite traditional:
{{ expression | filter1 | filter2 }}
{{ expression | filter1 | filter2 }}
Learn more about filters in the developer’s guide .
Modules
Applications in AngularJS do not have a main executable method. Instead, the module serves as a declarative description of how the application should be loaded. Due to this, for example, when writing test scripts, additional modules can be loaded, which will override some settings, thus facilitating end-to-end testing.
Learn more about the modules in the developer’s guide .
Testing
As the developers write, for testing they did a lot in AngularJS, so nothing can excuse you if you don’t test your application :)
Sample test e2e script:
describe('Buzz Client', function() { it('should filter results', function() { input('user').enter('jacksparrow'); element(':button').click(); expect(repeater('ul li').count()).toEqual(10); input('filterText').enter('Bees'); expect(repeater('ul li').count()).toEqual(1); }); });
Learn more about
unit testing and
e2e testing in the developer’s guide.
Angularjs batarang
This is an
extension for Chrome that makes it easy to debug AngularJS applications. Allows you to work with the hierarchy of the scope-s, allows you to profile the application, visualizes the dependencies between services, displays the contents of the scope-s on the item page, allows you to display and change values ​​in the scope from the console. A good text description on a
page in github . Good
youtube video.
What else to read