Slide 1

Slide 1 text

Carlos Hernando @chernando

Slide 2

Slide 2 text

Javascript

Slide 3

Slide 3 text

AngularJS

Slide 4

Slide 4 text

 7KH3ULQFLSOHV %RLOHUSODWH '5< 6WUXFWXUH 7HVWDELOLW\ Google I/O 2013 - Design Decisions in AngularJS http://youtu.be/HCR7i5F5L8c

Slide 5

Slide 5 text

AngularJS at a glance

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

View

Slide 8

Slide 8 text

Bootstrap ! ! ! ! ! /app/index.html

Slide 9

Slide 9 text

Directives ! ! ! ! ! ! ! ! /app/index.html

Slide 10

Slide 10 text

Data binding ! ! ! ! !

Hello {{ name }}

! ! ! ! /app/index.html

Slide 11

Slide 11 text

step 0

Slide 12

Slide 12 text

Application Structure 'use strict';! ! angular.module('tryitApp', []); /app/scripts/app.js

Slide 13

Slide 13 text

Directives /app/index.html

Slide 14

Slide 14 text

ng-repeat
!

{{ talk.title }}

! !

! {{ talk.starts }}! {{ talk.author }}!

! !

! {{ talk.description }}!

!
/app/index.html

Slide 15

Slide 15 text

Filters

! {{ talk.starts | date:'medium' }}! {{ talk.author | uppercase}}!

! ! /app/index.html

Slide 16

Slide 16 text

ng-repeat & filters ! !
! !

{{ talk.title }}

!

! …!

!

! …!

!
/app/index.html

Slide 17

Slide 17 text

Directives & Data-binding

! ! ! {{ talk.description }}!

/app/index.html

Slide 18

Slide 18 text

step 1

Slide 19

Slide 19 text

No JavaScript

Slide 20

Slide 20 text

Controllers & Services

Slide 21

Slide 21 text

Controller: ScheduleCtrl angular.module('tryitApp')! .controller('ScheduleCtrl',! function ($scope) {! $scope.talks = […];! }! );! /app/scripts/controllers/schedule.js

Slide 22

Slide 22 text

$scope

Slide 23

Slide 23 text

Controllers & Views ! !
! !
!

{{ talk.title }}

! …!
/app/index.html

Slide 24

Slide 24 text

Service: T alks angular.module('tryitApp')! .service('Talks', function Talks() {! var talks = [! {! 'id': 1,! 'title': 'Acto de Apertura',! 'starts': new Date('2012-03-17T10:00:00+0100'),! …! }, …! ];! ! this.query = function () {! return talks;! };! }); /app/scripts/services/talks.js

Slide 25

Slide 25 text

Dependency Injection angular.module('tryitApp')! .controller('ScheduleCtrl',! function ($scope, Talks) {! $scope.talks = Talks.query();! }! ); /app/scripts/controllers/schedule.js

Slide 26

Slide 26 text

step 2

Slide 27

Slide 27 text

Testing

Slide 28

Slide 28 text

Service: Favorites angular.module('tryitApp')! .factory('Favorites', function ($log) {! var favorites = [];! ! function addTalk(talk) {! …! }! ! function isIn(talk) {! …! }! ! return {! addTalk: addTalk,! isIn: isIn,! talks: function() {! return favorites;! },! };! });! /app/scripts/controllers/schedule.js

Slide 29

Slide 29 text

Test: Favorites describe('Service: Favorites', function () {! ! beforeEach(module('tryitApp'));! ! var Favorites,! talk1 = { id: 1 },! talk2 = { id: 2 };! ! beforeEach(inject(function (_Favorites_) {! Favorites = _Favorites_;! }));! ! it('should not add same talk twice', function () {! expect(Favorites.talks().length).toBe(0);! Favorites.addTalk(talk1);! Favorites.addTalk(talk1);! expect(Favorites.talks().length).toBe(1);! });! });! /test/spec/services/favorites.js

Slide 30

Slide 30 text

Adding Favorites angular.module('tryitApp')! .controller('ScheduleCtrl',! function ($scope, Talks, Favorites) {! $scope.talks = Talks.query();! $scope.favorites = Favorites;! }! ); /app/scripts/controllers/schedule.js

Slide 31

Slide 31 text

Adding Favorites

! {{ talk.starts | date:'medium' }}! {{ talk.author }}! ! ! Add to Favorites! ! !

/app/index.html

Slide 32

Slide 32 text

step 3

Slide 33

Slide 33 text

Routes & Views

Slide 34

Slide 34 text

Routes angular.module('tryitApp', ['ngRoute'])! .config(function ($routeProvider) {! $routeProvider! .when('/', {! templateUrl: 'views/schedule.html',! controller: 'ScheduleCtrl'! })! .when('/my-schedule', {! templateUrl: 'views/schedule.html',! controller: 'MyScheduleCtrl'! })! .otherwise({! redirectTo: '/'! });! });! /app/scripts/app.js

Slide 35

Slide 35 text

ng-view
!
! /app/index.html

Slide 36

Slide 36 text

views/schedule.html

Talks

!

! !

!
!

{{ talk.title }}

!

! {{ talk.starts | date:'medium' }}! {{ talk.author }}! ! Add to Favorites! !

! !

! ! {{ talk.description }}!

!
! /app/views/schedule.html

Slide 37

Slide 37 text

MyScheduleCtrl angular.module('tryitApp')! .controller('MyScheduleCtrl',! function ($scope, Favorites) {! $scope.talks = Favorites.talks();! $scope.favorites = Favorites;! }); /app/scripts/controllers/myschedule.js

Slide 38

Slide 38 text

step 4

Slide 39

Slide 39 text

More… • Directives • Providers • Config • Constants • Directives • $watch & $apply • Protractor • Other modules: • Resource • Animation • … • …

Slide 40

Slide 40 text

Books

Slide 41

Slide 41 text

Questions?!?

Slide 42

Slide 42 text

https://github.com/chernando/tryit_angularjs/ https://speakerdeck.com/chernando/try-angularjs

Slide 43

Slide 43 text

Thanks!

Slide 44

Slide 44 text

About This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http:// creativecommons.org/licenses/by-nc-sa/3.0/. Product names, logos and trademarks of other companies which are referenced in this document remain the property of those other companies.