On Github kristofmic / angular_tut
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>
$scope is only used in our Controllers and Directives, where we bind data to a view
The $rootScope object is the parent of all $scopes
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!';
});
$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>-->
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();
}
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();
});
<my-directive></my-directive> <div my-directive></div> <div class="my-directive"></div> <!-- directive: my-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
}
}
{{ object_expression | filterName : expression : comparator}}
Filters can be used in JavaScript via the $filter service
$filter('filterName')(object, expression, comparator)
Filters do not alter the underlying data
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>-->
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'));
});
Set stubbed out or mocked out services
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
})
}));
Write your tests!
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!');
});