Angular-JS is an open-source MVC framework created by Miško Hevery and Adam Abrons in 2009 which has been bought and is now maintained by Google.
At first sight, nothing new: EmberJS has already been doing this for a long time, so what’s all the fuss about it ?
Much like EmberJS, AngularJS allows to bind parts of the interface to the data. The difference lies in how the data is linked and updated in the UI.
In EmberJS, the developer has to create special objects and use special methods to update the data so that EmberJS knows when and how to update the data:
var person = this.get("model"); person.set('firstName', 'James');
While this works, the developer has to use special objects and special methods.
AngularJS takes a different approach: instead of having to define special objects, any object can be used as binding and the view will be magically updated:
<div ng-controller="MyCtrl"> <p>firstName: !</p> </div>
// within your controller $scope.firstName = 'James';
Scope is Magic
I say magically but actually it’s a bit more complicated: for the framework to update your view, you have to make your changes in a special method called “apply”. The good thing is that most Angular methods already take care of this so you usually don’t have to worry about it: since these methods wrap your function calls inside apply, Angular will be aware of your changes without having to do anything.
When executing code outside AngularJS methods, you’ll have to manually wrap your callbacks into scope.$apply() calls. This can become quite complicated and this is an important concept of AngularJS so I recommend reading this article by Jim Hoskings: AngularJS and scope.$apply
A first app using AngularJS
Whenever I discover a new tool, I try to create a new project to familiar myself with the new library. A few years ago, I wrote a little app to experiment with CSS3/HTML5: the app simply fetched a photo feed from the popular 500px website and presented the user with nice CSS 3 effects (and even mp3 using the Audio tag: wow! ;)).
Converting this small application to AngularJS sounded like a good first project. The application makes use of Ajax, LocalStorage to store settings and some jQuery bindings for user interaction: not too complicated, nor too easy!
After a few afternoons I had most of the app rewritten: once you understand the AngularJS concepts, it’s really easy to get something working! The fact that DOM manipulation can be done using jQuery-like syntax and the numerous articles covering AngularJS also helped a lot.
I also used the following very handy building/development tools while creating this project:
- NodeJS and Npm