10
handson handson

Hands on AngularJS

Embed Size (px)

DESCRIPTION

An introduction to the key patterns of angular test-driven development using testacular, services and directives.

Citation preview

Page 1: Hands on AngularJS

handsonhandson

Page 2: Hands on AngularJS

handson

keyargumentswhy angular?

2 - Way Binding

<div ng-controller="SearchController"> <input type="text" ng-model="searchTerm"></div>

app.controller("SearchController", function($scope){

// Initially set the search term to hello $scope.searchTerm = "Hello"; // Watch for changes of the search term $scope.$watch("searchTerm", function(){ // perform search });});

HTML JS

Page 3: Hands on AngularJS

handson

keyargumentswhy angular?

Directives

<thumbnail picture="pictures.find(2)"></thumbnail>

app.directive("thumbnail", function(){ return { restrict: 'E', scope:{ picture: '=' }, replace: true, template: "<div class='thumbnail' ng-click='toggle()'>" + "<img src='images/{{picture.url}}' alt='{{picture.name}}'></img>" + "</div>", controller: function($scope, element){ $scope.selected = false; $scope.toggle = function(){ $scope.selected = !$scope.selected; } }, };});

HTML

JS

Page 4: Hands on AngularJS

handson

keyargumentswhy angular?

Dependency Injection

JS

app.controller("SearchController", function($scope, $http, ...){

// Use the $scope // Use whatever you need // Use $http $http.get("/search/" + $scope.searchTerm).success(function(data){

$scope.results = data; });

});

Page 5: Hands on AngularJS

handson

keyargumentswhy angular?

Testability!

JS

describe("Pictures Service", function(){ var pictures; beforeEach(function(){ module("gallery"); module("galleryMock"); inject(function($injector){ pictures = $injector.get("pictures"); }); }); it("returns 4 default pictures", function(){ expect(pictures.all.length).toBe(4); });});

Page 6: Hands on AngularJS

handsonhandson

lets code!

Page 7: Hands on AngularJS

handson

projectrepositorystart using

https://github.com/southdesign/hands-on-angular

https://github.com/southdesign/hands-on-angular/commits/master

projectcommitsstep by step

Page 8: Hands on AngularJS

handson

galleryscreenshotfinal project

Page 9: Hands on AngularJS

handson

sourcecodefinal project

Page 10: Hands on AngularJS

handsonthankyou

we’re done!

Thomas Fankhauser, [email protected]

try it out on: https://github.com/southdesign/hands-on-angular