January 20, 2013
AngularJS is an open-source JavaScript framework. It extends HTML’s syntax to express your application’s components clearly and define AngularJS functions, for example:
AngularJS automatically synchronizes data from your UI (view) with your JavaScript objects (model) through 2-way data binding. It also helps with server-side communication, taming async callbacks with promises and deferreds; and make client-side navigation and deeplinking with hashbang urls or HTML5 pushState a piece of cake.
First of all, create a new Rails app, and change your html-tag to <html ng-app>
. To setup AngularJS link to the newest version (see angularjs.org) and include your app/assets/javascripts/application.js after that in your layout file (eg: application.html.erb):
You have to intercept html requests to create a single-page application. If you have your root (eg: home#index) you can easily put a before_filter in your ApplicationController to render ‘home/index‘ or even another layout every time the request format is HTML.
I recommend to read the conceptual overview. Understand Angular’s vocabulary and how all the Angular components work together. Watch the AngularJS Tutorial (with node.js). It explains every major AngularJS feature and also gives a live demo.
Create a new JS File (or use your application.js) and try creating your first AngularJS controller.
JS:
The data is now hardcoded in the controller, later $scope.shows will be defined with data from an external API. (you can also use rails routes and respond_to :json if you use Rails Models)
HTML:
As you see, you can define one part of your site to use AngularJS – and you can make many separated controllers.
To load real data by ajax call, you need to add $http in your controller function:
For example, you would like to add localstorage functionality to your angular app. First look for an angular module, before you write one by yourself ;) –> github.com/grevory/angular-local-storage
Include the external JS file in your app/assets/javascripts folder – you can see this line on the top of the file:
Now it’s time to define your own app (appname) and add this module (LocalStorageModule). You can add as many modules as you like. In ‘LocalStorageModule’ a service called ‘localStorageService’ is defined. To use this service you have to include it in your controller initialization next to $scope and $http.
Last step: update the html-tag and define the app name. Now you can use ‘localStorageService’ in your controller.
Below are some code snippets with examples how to use it (in comments above every function).
There can be a minification problem if you use Rails with AngularJS (the Rails Asset Pipeline minifies all asset files). I got an error when deploying on heroku:
Uncaught Error: Unknown provider: e
before I did this: (not wrong, but leads to an error!)
According to AngularJS tutorial (docs.angularjs.org/tutorial/step_05) you can change this line to the following to prevent minification issues:
I hope I’ve helped some of you to get started with AngularJS and Rails.
Happy coding!