Ultimate AngularJS Cheat Sheet
Ultimate AngularJS Cheat Sheet
});
var mod = angular.module(name);
mod.run(function run(...) {
});
Application Module Usage <... ng-app=name></...>
How to load AngularJS on the HTML tag
Resources
How to load AngularJS on the BODY tag
How to load AngularJS on a directive
How to load AngularJS fully encapsulated
Who Else Wants to Load Multiple Angular Apps at
Once
Official Documentation
Controllers
Define
mod.controller(CtrlName,
function CtrlName(...) {
});
mod.controller(CtrlName,
[..., function CtrlName(...) {
}]);
Standard Usage
Controller As Usage
Resources
mod.controller(CtrlName,
function CtrlName(...) {
// hang everything off the instance
// of the controller so we are compatible
// with the Controller As syntax
var vm = this;
});
<... ng-controller=CtrlName></...>
<... ng-controller=CtrlName as vm></...>
Official Documentation
Directives
Define
mod.directive(name,
function(...) {
return {
restrict: 'AEC',
scope: {},
template: 'some html',
templateUrl: 'path/to/template',
replace: true OR false,
transclue: true OR false,
require: 'directiveName',
link: linkFn
};
});
function linkFn(scope, elem, attr,
controller, transcludeFn) {
Resources
}
Restrict Your Directives Like a Boss
What Every Developer Should Know About Isolate
Scope
What Everybody Needs to Know About Isolate
Scope Binding
Resources
mod.service(name,
function name(...) {
// add functions and properties to
// the instance of this service
this.fn = function fn() {
};
});
Official Documentation
Factories
Define
Resources
mod.factory(name,
function name(...) {
// return an object from the factory
// to provide as the injectable
return {
fn: function fn() {
}
};
});
Official Documentation
Providers
Define
mod.provider(name,
function name(...) {
// add configuration functions and
// properties that can be modified in
// the module.config function
this.configFn = function configFn(...) {
};
this.configProp = default value;
Resources
Constants
Define
Resources
UI Router
Configure
app.config(function config(
$stateProvider, $urlRouterProvider) {
$stateProvider
.state(home, {
url: /,
templateUrl: path/to/template
});
$urlRouterProvider.otherwise('/');
}
Named View
.state('name', {
url: 'url',
views: {
'ui-view-name': {
templateUrl: 'path/to/template'
}
}
})
.state('parentName.childName', {
Child State
url: 'relative/url/to/parent',
templateUrl: 'path/to/template'
})
Named View Replacement .state('parentName.childName', {
url: 'url',
views: {
'ui-view-name@ancestorName': {
templateUrl: 'path/to/template'
}
}
})
The Secret of UI Router Resolvers
Resources
UI Router: What Everybody Should Know About
Child States
UI Router: A Secret About Named Views
UI Router with Mike eBook
Official Documentation