Angular

Bootstrap Angular application

<div ng-app="appName"></div>

Modules

Create a module that depends on module1 and module2

angular.module('app', ['module1', 'module2']);

Get reference of a module named app

var app = angular.module('app')

Filters

angular.module("myModule").filter("squareNumber", function(){
    return function(num){
        return num * num;
    };
});

Usage:

<span> {{value || squareNumber }} </span>

Options

Controllers

angular.module("myModule").controller("SampleController", function($scope){
    $scope.songTitle = "Hand of Doom";
});

In the view

<div ng-controller="SampleController">
    <div>{{songTitle}}</div>
</div>

Services

Services in Angular are singletons. Only one instance of the service is created in the Angular application’s lifetime.

angular.module("myModule").service("sampleService", function(){
    var svc = this;
    var songTitles = ["Here Comes The Pain", "South of Heaven", "Raining Blood", "Skeleton Christ"];

    svc.addSong = function(song){
        songTitles.push(song);
    };
    svc.getSongTitles = function(){
        return songTitles;
    }
});

Directives

myModule.directive("directiveName", function (injectables) {
    return {
        restrict: "A",
        template: "<div></div>",
        templateUrl: "directive.html",
        replace: false,
        transclude: false,
        scope: false,
        require: ["someOtherDirective"],
        controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... },
        link: function postLink(scope, iElement, iAttrs) { ... },
        priority: 0,
        terminal: false,
        compile: function compile(tElement, tAttrs, transclude) {
            return {
                pre: function preLink(scope, iElement, iAttrs, controller) { ... },
                post: function postLink(scope, iElement, iAttrs, controller) { ... }
            }
        }
    };
})

//Update this section based on docs.angularjs.org

Built-in Directives

Routes

myModule.config(function($routeProvider){
    $routeProvider.when("/home", {templateUrl:"templates/home.html",
                                  controller: "HomeController"}).when("/details/:id", {template: "templates/details.html",                                       controller:"ListController"})
                .otherwise({redirectTo: "/home"});
});

Register Services

Service: module.service( 'serviceName', function );

Factory: module.factory( 'factoryName', function );

Provider: module.provider( 'providerName', function);

Utility Functions