Here's all you need to know about Angular 2, the exciting new successor to Google's wildly popular JavaScript framework, AngularJS
Two years ago Google promised a major rewrite of AngularJS, which back then was already topping the charts as the most popular JavaScript framework. Now the new version, dubbed Angular 2, is finallyΒ nearing a beta release.
Angular 2 draws on the perspectives of experts who have both learned from experience and have an eye toward future standards.Β Nowβs a great time to see what the framework authors are bringing to the next version.
The rise of Web Components
In AngularJS, nearly everything to control page rendering is done with directives. There are lots of existing directives ranging from the very simple (for example, <a href="https://docs.angularjs.org/api/ng/directive/ngIf" rel="noopener nofollow" target="_blank">ngIf</a> is a directive you can use to conditionally show/hide part of the page) to the complex (such as data binding with <a href="https://docs.angularjs.org/api/ng/directive/ngBind" rel="noopener nofollow" target="_blank">ngBind</a>). You often create your own directives to add custom behavior to an element or simply to render a template.
Angular 2 largely replaces directives with βcomponents.β This approach looks forward to future Web Components specifications, including Shadow DOM and HTML Imports. Meanwhile, the component pattern is already showing up in many JavaScript libraries.
When you compare AngularJSβs directive approach with Angular 2βs component approach side by side, they appear very similar. But conceptually, theyβre different:
As a standard, Web Components have some issues. Browser vendors still need to shake things out a bit for Web Components to be implemented. As an architectural component of an client-side application, however, the Web Components approach is something to be reckoned with.
Componentization within the interface allows behavior and presentation to be modularized, testable, and reusable. Furthermore, the developers of Angular 2 plan to make it compatible with Web Components created with such libraries as Polymer and X-tag so that Angular 2 will be very friendly to code reuse.
The bottom line is that, once the kinks are ironed out, youβll want to use Web Components. As it turns out, using a performant framework β as Angular 2 intends to be β may be one of the best ways to incorporate Web Components into your applications, while letting the framework authors and the community manage compatibility with evolving standards.
Reactive programming
Angular 2 builds on a reactive programming style using event-driven streams of data, rather than on the object-oriented approach you might see in MV* frameworks. In fact, data flow is the most interesting thing you wonβt see mentioned on Angular 2βs features page.
It all starts with how the framework gets its data: Promises versus Observables. Promises are a method of triggering an asynchronous action, then handling the result when itβs ready. In contrast, Observables are a proposed standard type that allows subscription to a stream of values.
In his talk on data flow in Angular 2, Rob Wormald provides examples of βtypeaheadβ functionality in AngularJS versus Angular 2, demonstrating the work required to debounce requests β that is, to not waste HTTP requests while the user is still typing, yet still provide nearly instantaneous βtypeaheadβ search results.
Iβve modified the code here to make it more understandable out of context. The value of this.searchResults is modified based on a typeahead field.
As you can see, a simple reactive action (debounceTime) is required for Angular 2 to provide the same debounce functionality as AngularJS to get values into this.searchResults.
In order to leverage Observables and reactive actions, Angular 2 employs the next version of RxJS. That means users of Angular 2 also benefit from access to this reactive tooling (the ability to simply debounce a stream of events serving as an example), and Angular 2 users are likely provide more fuel for reactive programming champions.
Choose your own language
While AngularJS is a JavaScript framework, Angular 2 is a βwhatever compiles to JavaScriptβ framework. As long as what you want to write in can compile to JavaScript, you can (probably) use Angular 2.
In the new Angular documentation, youβll notice a drop-down for JavaScript, TypeScript, or Dart. While this might change (it looks like a beast of documentation work), itβs an indicator of the communityβs intentions.
Angular 2 itself is written in TypeScript, a typed superset of JavaScript. You can think of it in shorthand as βES2015 with types.β While this may give TypeScript an edge in adoption, itβs clear that the framework authors intend Angular 2 to be friendly to transpiled languages in general.
Thanks to AngularJSβs popularity, adoption of Angular 2 will probably be huge. The new framework has a lot to show you regarding trends to pay attention to: JavaScript apps wonβt have to be written in JavaScript (if they can make the compile target work). Reactive programming is on the rise, especially for browser apps. And the Web Components standard is sticking around β even if the browser vendors havenβt ironed out how they will support it.


