An easy introduction to AngularJS

AngularJS doesn't do much. When you take it out of the box (i.e. load it on a webpage) you're only given five things to play with: modules, services, controllers, directives, and filters.

Each of these things complements the other. There are very slim overlaps in functionality between all the moving pieces of AngularJS. That fills my heart with joy because I only have to learn how to do something one way, with one slice of the AngularJS pie.

The funny thing about AngularJS is when you use every thing it provides, you get something far greater than its sum. In that way AngularJS truly does allow you to create web-apps of 'superheroic' proportions. Their words, not mine (although I do agree).

Ready to dive a whee bit deeper?

Modules

A module in AngularJS is about what you'd expect.

It lets you package your code into small chunks, useful for breaking a large application into smaller pieces and for distributing code.

You define a module name upon instantiation, neatly namespacing all the relevant code within. This helps you stay clear of polluting the global window object and not worry about clobbering other variables.

One of the most lovely features of modules in AngularJS is that you can load parts of a module in any order.

For example on a html page that is loading your application you can load the script tags in any order, and it won't affect how the application is put together.

Services

In their simplest form, a service provides singleton objects to your application. That's really about all they do.

Sure there are some nuances to services. There's different variants: factories, providers, constants, and values. Most of them are quite aptly named.

Constants and values do about what you'd expect. They are easy to use key-value stores that can't be modified after app start-up. Hence the 'constant' name.

Factories are services designed to allow you to create new instances of an object. The factories themselves are singletons, but they're designed to allow you to create as many instances of an object as you desire.

Providers are services that allow an amount of configuration before application initialization. For example if you have an API service, you can change whether it points to the dev or production server by using the provider pattern (as opposed to having that if statement within the service. Always better to be explicit.).

That's about the skinny of what services are in AngularJS. These things should (almost) never touch the DOM. They're just vanilla JavaScript objects, with sugar around them to work neatly amidst the AngularJS framework.

Controllers

Traditionally, controllers are used as the glue between models and views. Here again AngularJS doesn't stray far from tradition. In an AngularJS controller you can define what data and behavior is exposed to the view. How this is done is semi-magical.

There's a service that AngularJS provides called '$scope'. $scope is used like a regular JavaScript object. You can assign strings ($scope.hello = 'world';), other objects ($scope.person = {name: 'bob'};), and functions ($scope.foo = function() { return 'bar'; }). You know...whatever you want.

Where things get a little opaque is how that gets exposed to the view. If you're really curious you're free to dive into AngularJS internals. However it is safe to assume that anything assigned to $scope is available for access in the view.

Aside from the magic of $scope everything else about a controller is mundane. Thank goodness.

Directives

Basically directives are components. That's the simplest way to understand it.

Past that things can get awfully confusing awfully quickly.

Directives allow you to extend the language of the DOM, doing things that jQuery is commonly used for. For example AngularJS defines the ngClick directive, which allows you to attach behavior when a click event is observed. When AngularJS compiles your views (which are basically HTML) it has knowledge of the ngClick attribute, and its behavior.

So when the AngularJS compiler sees <div ng-click="popup()"></div> it attaches a click event listener on that div element (as defined in the ngClick directive).

AngularJS ships with many great default directives. It's encouraged to re-use directives within directives.

Just remember that directives are similar to components: they allow you to create a new unit with specific functionality.

Filters

Filters are used by templates to modify the output of data.

For example AngularJS ships with the number filter which can take an integer input such as 12345 and output it with commas as 12,345.

Or you can have an array of strings and only want to show the ones that start with a 'he'. You'd use the filter filter (unfortunate name redundancy).

Wrap-up

The dirty secret of AngularJS is that it's basically a simple set of tools that allow you to do advanced things really easily.

AngularJS only gets really complicated when you start diving into its source code. But let's stay away from that elephant in the corner.

There's an interesting learning curve when you get up to speed with AngularJS. The first week you'll have a grand old time. The second week you'll encounter some weirdness which will throw you back a bit. By the third week you'll have to do a little more conceptual diving into AngularJS to understand why things are behaving the way they are.

By the fourth week you'll be able to write a blog post just like this.

Get out there super-heroes. Start writing some super-heroic JavaScript.


comments powered by Disqus