AngularJS

From Infogalactic: the planetary knowledge core
Jump to: navigation, search
AngularJS
AngularJS logo
Developer(s) Google
Initial release October 20, 2010; 13 years ago (2010-10-20)[1]
Stable release 1.5.6 / May 27, 2016; 7 years ago (2016-05-27)
Development status Active
Written in TypeScript, JavaScript
Platform Cross-platform, see Legacy browser support
Size 144 KB production
1 MB development
Type JavaScript/SPA framework
License MIT License
Website www.angularjs.org
angular.io

AngularJS (commonly referred to as "Angular" or "Angular.js") is an open-source client-side web application framework mainly maintained by Google and by a community of individuals and corporations to address many of the challenges encountered in developing single-page applications. It aims to simplify both the development and the testing of such applications by providing a framework for client-side model–view–controller (MVC) and model–view–viewmodel (MVVM) architectures, along with components commonly used in rich Internet applications.

The AngularJS framework works by first reading the HTML page, which has embedded into it additional custom tag attributes. Angular interprets those attributes as directives to bind input or output parts of the page to a model that is represented by standard JavaScript variables. The values of those JavaScript variables can be manually set within the code, or retrieved from static or dynamic JSON resources.

According to JavaScript analytics service Libscore, AngularJS is used on the websites of Wolfram Alpha, NBC, Walgreens, Intel, Sprint, ABC News, and approximately 8,400 other sites out of 1 million tested in July 2015.[2]

AngularJS is the frontend part of the MEAN stack, consisting of MongoDB database, Express.js web application server framework, Angular.js itself, and Node.js runtime environment.

Philosophy

AngularJS is built on the belief that declarative programming should be used to create user interfaces and connect software components, while imperative programming is better suited to defining an application's business logic.[3] The framework adapts and extends traditional HTML to present dynamic content through two-way data-binding that allows for the automatic synchronization of models and views. As a result, AngularJS de-emphasizes explicit DOM manipulation with the goal of improving testability and performance.

AngularJS's design goals include:

  • to decouple DOM manipulation from application logic. The difficulty of this is dramatically affected by the way the code is structured.
  • to decouple the client side of an application from the server side. This allows development work to progress in parallel, and allows for reuse of both sides.
  • to provide structure for the journey of building an application: from designing the UI, through writing the business logic, to testing.

Angular implements the MVC pattern to separate presentation, data, and logic components. Using dependency injection, Angular brings traditionally server-side services, such as view-dependent controllers, to client-side web applications. Consequently, much of the burden on the server can be reduced.

Scope

Angular uses the term "scope" in a manner akin to the fundamentals of computer science.

Scope in computer science describes when in the program a particular binding is valid. The ECMA-262 specification defines scope as: a lexical environment in which a Function object is executed in client-side web scripts;[4] akin to how scope is defined in lambda calculus.[5]

As a part of the "MVC" architecture, the scope forms the "Model", and all variables defined in the scope can be accessed by the "View" as well as the "Controller". The scope behaves as a glue and binds the "View" and the "Controller".

In Angular, "scope" is a certain kind of object[6] that itself can be in scope or out of scope in any given part of the program, following the usual rules of variable scope in JavaScript like any other object.[7] When the term "scope" is used below, it refers to the Angular scope object and not the scope of a name binding.

Bootstrap

The tasks performed by the AngularJS bootstrapper occur in three phases[8] after the DOM has been loaded:

  1. Creation of a new Injector
  2. Compilation of the directives that decorate the DOM
  3. Linking of all directives to scope

AngularJS directives allow the developer to specify custom and reusable HTML-like elements and attributes that define data bindings and the behavior of presentation components. Some of the most commonly used directives are:

ng-app
Declares the root element of an AngularJS application, under which directives can be used to declare bindings and define behavior.
ng-bind
Sets the text of a DOM element to the value of an expression. For example, <span ng-bind="name"></span> displays the value of ‘name’ inside the span element. Any change to the variable ‘name’ in the application's scope reflect instantly in the DOM.
ng-model
Similar to ng-bind, but establishes a two-way data binding between the view and the scope.
ng-model-options
Provides tuning for how model updates are done.
ng-class
Lets class attributes be dynamically loaded.
ng-controller
Specifies a JavaScript controller class that evaluates HTML expressions.
ng-repeat
Instantiate an element once per item from a collection.
ng-show & ng-hide
Conditionally show or hide an element, depending on the value of a boolean expression. Show and hide is achieved by setting the CSS display style.
ng-switch
Conditionally instantiate one template from a set of choices, depending on the value of a selection expression.
ng-view
The base directive responsible for handling routes that resolve JSON before rendering templates driven by specified controllers.
ng-if
Basic if statement directive that allow to show the following element if the conditions are true. When the condition is false, the element is removed from the DOM. When true, a clone of the compiled element is re-inserted
ng-aria
A module for accessibility support of common ARIA attributes.
ng-animate
A module provides support for JavaScript, CSS3 transition and CSS3 keyframe animation hooks within existing core and custom directives.

Since ng-* attributes are not valid in HTML specifications, data-ng-* can also be used as a prefix. For example, both ng-app and data-ng-app are valid in AngularJS.

Two-way data binding

AngularJS' two-way data binding is its most notable feature, largely relieving the server backend of templating responsibilities. Instead, templates are rendered in plain HTML according to data contained in a scope defined in the model. The $scope service in Angular detects changes to the model section and modifies HTML expressions in the view via a controller. Likewise, any alterations to the view are reflected in the model. This circumvents the need to actively manipulate the DOM and encourages bootstrapping and rapid prototyping of web applications.[9] AngularJS detects changes in models by comparing the current values with values stored earlier in a process of dirty-checking, unlike Ember.js and Backbone.js that trigger listeners when the model values are changed.[10]

Development history

AngularJS was originally developed in 2009 by Misko Hevery[11] at Brat Tech LLC[12] as the software behind an online JSON storage service, that would have been priced by the megabyte, for easy-to-make applications for the enterprise. This venture was located at the web domain "GetAngular.com",[12] and had a few subscribers, before the two decided to abandon the business idea and release Angular as an open-source library.

Releases

Angular 1

The current stable release of AngularJS is 1.5.6.[13]

Angular 2

AngularJS 2.0 was announced at the ng-Europe conference 22-23. September 2014.[14] The drastic changes in the 2.0 version created considerable controversy among developers.[15] On April 30, 2015, the AngularJS developers announced that Angular 2 moved from Alpha to Developer Preview.[16] AngularJS 2 can be downloaded from the official website. Angular 2 moved to Beta in December 2015,[17] and the first release candidate was published in May 2016.[18]

The code names are super-power related, are composed of two hyphen-joined words, should sound "fun / crazy / cool", and are publicly submitted and voted by users.[19]

Angular 2 is not a version upgrade, but a complete rewrite. The primary differences in Angular 2 over Angular 1 are:[20]

  • Mobile development - desktop development is much easier when mobile performance issues are handled first
  • Modularity - much core functionality has moved to modules, producing a lighter, faster core
  • Modern browsers only - reducing the need for browser compatibility workarounds
  • Angular 2 recommends the use of Microsoft's TypeScript language, which introduces the following improvements:
  • TypeScript is a superset of ECMAScript 6 so is therefore backwards compatible with ECMAScript 5 (i.e.: JavaScript). So Angular 2 also includes the benefits of ES6:
    • Iterators
    • For/Of Loops
    • Python-Style Generators
    • Reflection
  • Improved Dependency Injection - bindings make it possible for dependencies to be named
  • Dynamic Loading
  • Asynchronous Template Compilation
  • Simpler Routing
  • Diary.js Logging - measures where time is spent, to identify bottlenecks[21]
  • Replacing controllers and $scope with components and directives. (A component is a directive with a template.)

Legacy browser support

Versions 1.2 and later of AngularJS do not support Internet Explorer versions 6 or 7.[22] Versions 1.3 and later of AngularJS dropped support for Internet Explorer 8.[23]

Chrome plugin

In July 2012, the Angular team built a plugin for the Google Chrome browser called Batarang,[24] that improves the debugging experience for web applications built with Angular. The extension aims to allow for easy detection of performance bottlenecks and offers a GUI for debugging applications.[25] For a time during late 2014 and early 2015, the extension was not compatible with recent releases (after v1.2.x) of Angular.[26] It is currently, as of early 2016, still under development and works with all versions of Angular.

Performance

AngularJS sets out the paradigm of a digest cycle. This cycle can be considered a loop, during which AngularJS checks if there is any change to all the variables watched by all the $scopes. So, if $scope.myVar is defined in a controller and this variable was marked for watching, AngularJS will monitor the changes on myVar in each loop iteration.

This approach potentially leads to slow rendering when AngularJS checks on too many variables in the $scope every cycle. Hevery suggests keeping fewer than 2000 watchers on any page.[10]

See also

References

  1. Earliest known releases
  2. http://libscore.com/?#angular
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.
  10. 10.0 10.1 Lua error in package.lua at line 80: module 'strict' not found.
  11. Lua error in package.lua at line 80: module 'strict' not found.
  12. 12.0 12.1 Lua error in package.lua at line 80: module 'strict' not found.
  13. https://github.com/angular/angular.js/blob/master/CHANGELOG.md#152-differential-recovery-2016-03-18
  14. Lua error in package.lua at line 80: module 'strict' not found.
  15. Lua error in package.lua at line 80: module 'strict' not found.
  16. Lua error in package.lua at line 80: module 'strict' not found.
  17. http://angularjs.blogspot.it/2015/12/angular-2-beta.html
  18. Lua error in package.lua at line 80: module 'strict' not found.
  19. Lua error in package.lua at line 80: module 'strict' not found.[dead link]
  20. "7 key differences between Angular 1 and Angular 2"
  21. Lua error in package.lua at line 80: module 'strict' not found.
  22. Lua error in package.lua at line 80: module 'strict' not found.
  23. Lua error in package.lua at line 80: module 'strict' not found.
  24. Lua error in package.lua at line 80: module 'strict' not found.
  25. Lua error in package.lua at line 80: module 'strict' not found.
  26. Lua error in package.lua at line 80: module 'strict' not found.

Further reading

  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.

External links