Review: Nearly six months with Angular 2

Angular 2 is at rc.5 as I write this.  There is not quite a 2.0 release.

After nearly six months working with the various release candidates, here's my review of using using Angular 2 to largely re-build an Angular 1 app and integrate it into a mixed Angular 1 and 2 environment.    This will give you a high level overview of what is different in building an app for Angular 2 and serve as a launch pad into your own research.

This article will be helpful if you are looking on starting a new project and trying to work out whether to start with Angular 2, migrate from 1 to 2, go React, or something else.


TLDR;   There’s lots of good stuff In Angular 2: Components are first class; JavaScript modules and classes are used everywhere; Promises are replaced by RxJS.  You can migrate from Angular 1 reasonably easily. Write in TypeScript, Dart or JavaScript.  Basically Solid. Expect a few small changes before release. More software engineering than browser hacking.


What is all this about Components?

Each generation of web framework brings something new to help with the more complex challenges we face in software engineering in the browser.  Where Angular 1 arose to answer the question “How do I build a complete fully-featured single-page app?”, both Angular 2 and React both arrive to answer the additional question  “How do I build a complete fully-featured single-page app in a maintainable way, using real software engineering?” Components just seem like the obvious next step as we build more complex interfaces made of many parts.  React does this in a less bundled chose-your-own-bits way; Angular 2 in a bit more complete framework kind of way.

I'd say you really want to build component-based if you are doing anything more than making a quick one page website.  You need a component-based approach If you are looking to re-use bits of your app, work with a style guide, or have more than trivial QA/testing.  

As implemented by Angular 2, a component can contain code (JavaScript, TypeScript or Dart), an HTML Template, and one or more CSS files. HTML and CSS can be inline text in the component source or referenced files that are imported at compile time.  The way compiling and bundling works, you end up with these assets specific to the component embedded in the compiled component itself.   Nice.  Makes bundling easy, makes it easy to work on a single bit.

So, your component ends up with an HTML template string compiled in and if you choose, an array of CSS strings compiled in.  When the component is rendered by Angular 2, the component’s CSS is written to a head style tag and targeted with a unique attribute for this component only.

This does make it nice and easy to pick up a component, satisfy its dependencies and test it as an individual item.

Less a Global Soup

Angular 1 always gave me an uneasy feeling. The lack of real modules in JavaScript coupled with all these named strings, plus the sometimes-dashed sometimeCamelcase naming system left a lot of room for duh! Errors, conflicts and confusion.

This has all changed.  You have to be explicit in component metadata about dependencies, directives to be used in templates, services to be provided, input and outputs.   Up until rc.5 that could mean quite a lot of metadata for a non-trivial component, however this latest release candidate provides (and requires use of) an angular Module abstraction to collect a bunch of services, directives etc together.  This looks good though I haven’t tried it yet.

Less global soup also means you can bootstrap multiple angular apps in one browser completely separately, or even bootstrap Angular 1 and 2 apps separately alongside each other.  It it easy to confident about this with Angular 2 because it just doesn’t appear to leak stuff all over the browser global space.

And, biggest of all, the need to be explicit, especially with the inputs and outputs of components, means the rendering is targeted on what has changed only.  Therefore it is faster.  We haven't measurd it, but it looks much quicker than Angular 1 for the same complex pages.

Using a language: TypeScript or Dart or JavaScript

Going from Angular 1 to 2 is a bit like going from jQuery to Angular 1.   There is a lot more formality in the framework and it feels a lot more like software and a lot less like hacking up something quick in the browser.  This just makes sense as the complexity of single page apps increase, and more and more logic moves into the front-end.  To handle this increased complexity, you have a choice of front-end languages: TypeScript, Google’s own Dart, or JavaScript.

Both TypeScript and Dart are typed languages.  This may be a shock for those in love with JavaScript’s nearly unlimited expressibility and type mutability, but y’know, it does make life easier for two reasons I can immediately see.  One: strong typing takes the pressure off unit tests -- you’ll catch a lot of errors simply by using types when compiling -- all those ‘this property should be in this object’ or 'this function should be called with this' are caught by the compiler.  This cuts down on a lot of boilerplate unit tests that you just don’t need. And secondly:  developers coming from, say, Java, and having a little JavaScript awareness will find particularly TypeScript a pretty comfortable language to move into the front-end with.

The Angular 2 docs samples have been delivered in the order of TypeScript, Dart, and then JavaScript.  So Typescript was an obvious place to start for me.   The Typescript tooling is solid.  It cross-compiles to JavaScript well, has a proper strict linter etc. There were the usual new-language annoyances in the beginning, but the results in the project are very very few in-code bugs we’re debugging in the page.

If you really do want to go with JavaScript -- I would not advise that -- you either need to be using ES6 or later or get very used to coding up module and class patterns by hand.

Going Reactive: Promises replaced by Reactive RxJS streams

The next step beyond promises has been taken up.  Most places where you’d expect a promise to be returned (eg http service calls) with an async. result will now provide an RxJS stream to subscribe to.  At the basic level, this is like a Promise that can return multiple values in a stream.  Like a then called multiple times.  There’s a brain wrench in here, a slight syntax change over promises, and done carefully you can end up with an app that has flexibility in the time domain. Connect it to websockets and it keeps changing as the inputs change.

An early design decision in this app we’re working on was to build a subject-based configuration system using RxJS Subjects.  This means that we can in real-time change configuration inside the app and it will change form as it needs to.   Change detection and publishing the change is all done by RxJS -- all our app has to do is subscribe to and listen for config changes.  We use this config to  propagate view changes, query results and display size changes amongst other things and the app just responds as it happens.   There’s no writing code to make things dynamic as such.   It will hurt the head initially if you haven't dealt with it before. This guide helps.

My Overall Review: The Gut Feel

Too many words already, so a bit of my gut feel to finish off.    This is a good framework and at rc.4 it is solid.  It feels maintainable. It is expressive, in that it is easy to get stuff done in the templates and in the component TypeScript itself.   There is a step up in seriousness -- be ready for that or go for something lighter-weight.  It is fast compared to Angular 1.  The reactive feel is enjoyable.

I’d pick it up for the next reasonably-sized project or maybe even a small one.

Where to go next?

I'd suggest from here you dive into the Angular 2 documentation over at angular.io, and try out the quickstart.