{{}}
or ng-bind
ng-model
directive$injector
service which does this automatically by inspecting the arguments of a function
angular.module('MyModuleName', [ModuleDependency1, md2, md3, ...]);
angular.module('MyModuleName')
.controller('MyController', function() {});
<html>
<head></head>
<body ng-app="MyModuleName">
<h1>Hello World</h1>
</body>
</html>
$scope.heading = 'Hello World!';
<body ng-app="MyModuleName" ng-app="MyController">
<h1>{{heading}}</h1> <!-- <h1>Hello World!</h1> -->
</body>
angular.module('MyModuleName')
.controller('MyController', function($scope, dependency) {
$scope.heading = 'Hello World!';
});
// Syntax for minification/uglification
angular.module('MyModuleName')
.controller('MyController', ['$scope', 'dependency', function($scope, dependency) {
$scope.heading = 'Hello World!';
});
{{}}
or ng-bind
and evaluated against $scope
$scope.heading = 'Hello World!';
$scope.updateHeading = function(newHeading) {
$scope.heading = newHeading;
}
<h1>{{heading}}</h1> <!-- <h1>Hello World!</h1>-->
<button ng-click="updateHeading('Foobar')">Update</button> <!-- Click -->
<h1>{{heading}}</h1> <!-- <h1>Foobar</h1>-->
new
, thus all data/logic should be bound to this
new
and can return anything (e.g., Functions, Objects, Arrays, simple values)
angular.module('MyModule')
.service('MyService', [myService]);
function myService() {
this.bar = 'bar';
this.doSomething = function doSomething() {
return 'foo' + this.bar;
}
}
angular.module('MyModule')
.controller('MyController', ['$scope', 'MyService', myController]);
function myController($scope, myService) {
$scope.heading = myService.doSomething();
}
<h1>{{heading}}</h1> <!-- <h1>foobar</h1>-->
angular.module('MyModule')
.factory('MyService', [myService]);
function myService() {
var bar = 'bar';
return {
doSomething: doSomething
}
function doSomething() {
return 'foo' + bar;
}
}
angular.module('MyModule')
.controller('MyController', ['$scope', 'MyService', myController]);
function myController($scope, myService) {
$scope.heading = myService.doSomething();
}
.config()
of your Module
angular.module('MyModule')
.provider('MyService', function() {
var bar = 'bar';
return {
setBar: function(newBar) { bar = newBar; },
$get: function() {
return {
doSomething: function() { return 'foo' + bar; }
};
}
};
});
angular.module('MyModule')
.config(function(MyServiceProvider) {
MyServiceProvider.setBar('baz');
})
.controller('MyController', function($scope, MyService) {
$scope.heading = MyService.doSomething();
});
ng-repeat
provides a repeater for displaying the same template over a collection of like elementsng-model
allows us to bind HTML inputs to Models on our $scopeng-click
allows us to handle click events on buttons, anchors or any other element, with the function bound only to the local $scopeng-href
/ng-src
allow us to dynamically set href and src attributes. Important since we want angular to resolve the href/src before it's clicked or the image downloadedng-class
allows us to dynamically add/remove classes to elementsng-show
/ng-hide
allow us to toggle the display property of elements based on a boolean (or truthy/falsy) valueng-if
will actually remove the element from the DOM and the $scope it createsng-switch
removes elements from the DOM based on a value (doesn't have to be boolean) and can apply to multiple elements like a switch statement ng-bind
is an alternative to {{}}
that prevents edge-cases where there is a brief flicker of the {{}}
before Angular resolves the valueng-include
allows you to pull in a template of HTML from the server or pre-loaded in the template cacherestrict
property on the Directive Definition Object (DDO)E
for Element/Tag,
A
for Attribute,
C
for Class,
M
for Comment,
Default to EA
<my-directive></my-directive>
<div my-directive></div>
<div class="my-directive"></div>
<!-- directive: my-directive -->
template
and templateUrl
are the two ways of associating DOM content with a Directive
function myDirective() {
return {
restrict: 'EA', // how the directive can be used
templateUrl: 'myDirective.html', // template HTML
scope: true, // how we setup the directive's scope
// true => new scope prototypally inherited from parent
// false => parent scope
// object literal => isolate scope where custom properties can be injected
link: function link(scope, elem, attrs) {}, // provides DOM access
controller: function myController($scope) {}, // like other controllers, except specific to the directive's scope
}
}
|
character
{{ object_expression | filterName : expression : comparator}}
$filter
service
$filter('filterName')(object, expression, comparator)
date
converts a date to a specified format and timezonelimitTo
limits the elements returned in the collection to the specified numberorderBy
orders the elements returned in the collection based on the predicatelinky
finds links in a snippet of text and converts them to actual Anchor linksfilter
function and return a function used to filter the object
angular.module('MyModule')
.filter('capitalize', function() {
return function capitalizer(text) {
return text.charAt(0).toUpperCase() + text.slice(1);
};
})
.controller('MyController', function($scope) {
$scope.heading = 'foobar';
});
<h1>{{heading | capitalize}}</h1> <!-- <h1>Foobar</h1>-->
$http
is a service that enables server-side communication via AJAX requests$http
is both a function, whereby an AJAX configuration object is passed in, and an object with convenience GET
, POST
, DELETE
, and PUT
methods (also exposes HEAD, JSONP, and PATCH)$q
service, with additional success
and error
convenience functions$q
is a simplified Promise library implementation that supports then
, catch
, finally
, and all
methods
function asyncGreet(name) {
var deferred = $q.defer();
setTimeout(function() {
if (okToGreet(name)) { deferred.resolve('Hello, ' + name + '!'); }
else { deferred.reject('Greeting ' + name + ' is not allowed.'); }
}, 1000);
return deferred.promise;
}
describe('MyController', function() {
beforeEach(module('ch.Main'));
});
beforeEach(module(function($provide) {
stubbedService = {
mockMethod: jasmine.createSpy('mockedMethod')
};
$provide.value('MyService', stubbedService);
}));
beforeEach(inject(function($rootScope, $injector, $controller) {
anotherService = $injector.get('anotherService');
scope = $rootScope.$new();
myController = $controller('MyController', {
$scope: scope,
anotherService: anotherService
})
}));
it('should exist', function() {
expect(myController).toBeDefined();
});
it('should default to the correct page', function() {
expect(browser.getCurrentUrl()).toContain('/home');
});
it('should set the correct heading', function() {
expect(element(by.css('.heading')).getText()).toEqual('Hello World!');
});