Slide 1

Slide 1 text

Orchestrating Apps by Composing Angular Directives Jeremy Fairbank jeremyfairbank.com @elpapapollo

Slide 2

Slide 2 text

Hi, I’m Jeremy jfairbank @elpapapollo pushagency.io blog.jeremyfairbank.com simplybuilt.com

Slide 3

Slide 3 text

Building applications is hard

Slide 4

Slide 4 text

Who is software for?

Slide 5

Slide 5 text

Who is software for? • Computers • Users • Programmers • Me • You

Slide 6

Slide 6 text

Competing Goals

Slide 7

Slide 7 text

Competing Goals • Performance • Maintainability • Shipping code • Readability • Scalability

Slide 8

Slide 8 text

Competing Goals • Performance • Maintainability • Shipping code • Readability • Scalability

Slide 9

Slide 9 text

Patterns make it “easier”

Slide 10

Slide 10 text

Why not use them with JavaScript frameworks?

Slide 11

Slide 11 text

Composition

Slide 12

Slide 12 text

What is composition?

Slide 13

Slide 13 text

What is composition?

Slide 14

Slide 14 text

What is composition?

Slide 15

Slide 15 text

– Wikipedia “The various visual elements, known as elements of design, formal elements, or elements of art, are the vocabulary with which the visual artist composes. These elements in the overall design usually relate to each other and to the whole art work.” What is composition?

Slide 16

Slide 16 text

– Wikipedia “The way in which something is put together or arranged. The combination of parts or elements that make up something.” What is composition?

Slide 17

Slide 17 text

Composition Create with small independent pieces.

Slide 18

Slide 18 text

Composition Embodiment of many design principles.

Slide 19

Slide 19 text

Design Principles

Slide 20

Slide 20 text

Design Principles • Modularity • Separation of concerns (SOC) • Loose coupling • High cohesion • Composition over inheritance • Ad nauseam…

Slide 21

Slide 21 text

Directives Review

Slide 22

Slide 22 text

Directives Review • Attach behavior to the DOM via attributes, elements, comments, or CSS classes • Componentize functionality with HTML • Create custom HTML elements

Slide 23

Slide 23 text

Directives Review • Attach behavior to the DOM via attributes, elements, comments, or CSS classes • Componentize functionality with HTML • Create custom HTML elements

Slide 24

Slide 24 text

Directives Review angular.module('myApp', []) .directive('nameTag', function() { return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; });

Slide 25

Slide 25 text

Directives Review angular.module('myApp', []) .directive('nameTag', function() { return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; });

Slide 26

Slide 26 text

Directives Review angular.module('myApp', []) .directive('nameTag', function() { return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; });

Slide 27

Slide 27 text

Directives Review angular.module('myApp', []) .directive('nameTag', function() { return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; });

Slide 28

Slide 28 text

Directives Review

Slide 29

Slide 29 text

Directives Review

Slide 30

Slide 30 text

Directives Review

Slide 31

Slide 31 text

Directives Review return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } };

Slide 32

Slide 32 text

Directives Review return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; Restrict to element declaration (i.e. ``) E - element A - attribute C - class name

Slide 33

Slide 33 text

Directives Review return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; Isolate scope @ - attributes = - bindings & - behavior/callbacks

Slide 34

Slide 34 text

Directives Review return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; Templates template - literal template templateUrl - url or script id for template

Slide 35

Slide 35 text

Directives Review return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } }; link manipulate DOM or set some defaults on scope controller add to scope too, preferably for exposing an API Set up

Slide 36

Slide 36 text

Directives Review return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } };

Slide 37

Slide 37 text

Directives Review return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } };

Supplying the attribute value

Slide 38

Slide 38 text

return { restrict: 'E', scope: { name: '@' }, template: 'Hello, my name is {{name}}', link: function(scope) { if (!scope.name) { scope.name = 'Joe Schmoe'; } } };

Directives Review Using the default value

Slide 39

Slide 39 text

Composition and Angular

Slide 40

Slide 40 text

Composition and Angular • Focus on smaller problems to solve a large problem • Apply design principles to directives • Build app in terms of directives • Decompose large directives into small reusable directives • Refactoring and adding new features becomes more manageable

Slide 41

Slide 41 text

But how do we fit directives together?

Slide 42

Slide 42 text

Data Flow

Slide 43

Slide 43 text

Data Flow App Directive Directive Directive Directive Directive Directive Directive Directive Directive

Slide 44

Slide 44 text

Data Flow • Connecting directives to build an “app” through the flow of data • Handling model data among directives • Many approaches with pros and cons App Directive Directive Directive Directive Directive Directive Directive Directive Directive

Slide 45

Slide 45 text

Data Flow 1. Shared/global state with prototypal scoping 2. Exporting API’s with directive controllers 3. Isolate scope bindings and callbacks 4. Event broadcasting and emitting App Directive Directive Directive Directive Directive Directive Directive Directive Directive

Slide 46

Slide 46 text

Prototypal Scoping

Slide 47

Slide 47 text

Prototypal Scoping app.directive('imageGallery', function() { return { scope: true, templateUrl: 'image-gallery.html' }; });

Slide 48

Slide 48 text

Prototypal Scoping app.directive('imageGallery', function() { return { scope: true, templateUrl: 'image-gallery.html' }; }); Create a child scope that “inherits” from parent scope

Slide 49

Slide 49 text

Prototypal Scoping app.controller('ImageCtrl', function($scope) { function createImage(src, text) { return { src: src, text: text, favorited: false }; } $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; });

Slide 50

Slide 50 text

Prototypal Scoping app.controller('ImageCtrl', function($scope) { function createImage(src, text) { return { src: src, text: text, favorited: false }; } $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; });

Slide 51

Slide 51 text

Prototypal Scoping app.controller('ImageCtrl', function($scope) { function createImage(src, text) { return { src: src, text: text, favorited: false }; } $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; });

Slide 52

Slide 52 text

Prototypal Scoping <main-image></main-image> <image-list></image-list> <image-favorites></image-favorites>

Slide 53

Slide 53 text

Prototypal Scoping <main-image></main-image> <image-list></image-list> <image-favorites></image-favorites>

Slide 54

Slide 54 text

Prototypal Scoping <main-image></main-image> <image-list></image-list> <image-favorites></image-favorites>

Slide 55

Slide 55 text

Prototypal Scoping <h3>Main Image</h3> <img width="160" ng-src="{{mainImage.src}}">

Slide 56

Slide 56 text

Prototypal Scoping <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div>

Slide 57

Slide 57 text

Prototypal Scoping <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div>

Slide 58

Slide 58 text

Prototypal Scoping <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div>

Slide 59

Slide 59 text

Prototypal Scoping <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div>

Slide 60

Slide 60 text

Prototypal Scoping <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul>

Slide 61

Slide 61 text

Prototypal Scoping <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul>

Slide 62

Slide 62 text

Prototypal Scoping <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul>

Slide 63

Slide 63 text

Prototypal Scoping app.directive('mainImage', function() { return { scope: true, templateUrl: 'image-gallery.html' }; });

Slide 64

Slide 64 text

Prototypal Scoping app.directive('imageList', function() { return { scope: true, templateUrl: 'image-list.html', link: function(scope) { scope.setMainImage = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.imageFavorites.push(image); } }; } }; });

Slide 65

Slide 65 text

Prototypal Scoping app.directive('imageFavorites', function() { return { scope: true, templateUrl: 'image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });

Slide 66

Slide 66 text

Prototypal Scoping app.directive('imageFavorites', function() { return { scope: true, templateUrl: 'image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });

Slide 67

Slide 67 text

Prototypal Scoping Demo

Slide 68

Slide 68 text

Prototypal Scoping What about more than one gallery?

Slide 69

Slide 69 text

Prototypal Scoping What about more than one gallery?

Slide 70

Slide 70 text

Prototypal Scoping app.controller('CatImageCtrl', function($scope) { $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; }).controller('NatureImageCtrl', function($scope) { $scope.images = [ createImage('/images/nature-1.jpg', 'Nature 1'), createImage('/images/nature-2.jpg', 'Nature 2'), createImage('/images/nature-3.jpg', 'Nature 3'), createImage('/images/nature-4.jpg', 'Nature 4') ]; });

Slide 71

Slide 71 text

Prototypal Scoping Demo

Slide 72

Slide 72 text

Prototypal Scoping What’s the problem?

Slide 73

Slide 73 text

Prototypal Scoping Object properties on prototypes are problematic

Slide 74

Slide 74 text

app.controller('ImageCtrl', function($scope) { // ... $scope.mainImage = {}; $scope.imageFavorites = []; }); app.directive('imageList', function() { return { // ... link: function(scope) { scope.setMainImage = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.imageFavorites.push(image); } }; } }; });

Slide 75

Slide 75 text

app.controller('ImageCtrl', function($scope) { // ... $scope.mainImage = {}; $scope.imageFavorites = []; }); app.directive('imageList', function() { return { // ... link: function(scope) { scope.setMainImage = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.imageFavorites.push(image); } }; } }; });

Slide 76

Slide 76 text

app.controller('ImageCtrl', function($scope) { // ... $scope.mainImage = {}; $scope.imageFavorites = []; }); app.directive('imageList', function() { return { // ... link: function(scope) { scope.setMainImage = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.imageFavorites.push(image); } }; } }; });

Slide 77

Slide 77 text

app.controller('CatImageCtrl', function($scope) { $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; }).controller('NatureImageCtrl', function($scope) { $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/nature-1.jpg', 'Nature 1'), createImage('/images/nature-2.jpg', 'Nature 2'), createImage('/images/nature-3.jpg', 'Nature 3'), createImage('/images/nature-4.jpg', 'Nature 4') ]; });

Slide 78

Slide 78 text

app.controller('CatImageCtrl', function($scope) { $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; }).controller('NatureImageCtrl', function($scope) { $scope.mainImage = {}; $scope.imageFavorites = []; $scope.images = [ createImage('/images/nature-1.jpg', 'Nature 1'), createImage('/images/nature-2.jpg', 'Nature 2'), createImage('/images/nature-3.jpg', 'Nature 3'), createImage('/images/nature-4.jpg', 'Nature 4') ]; });

Slide 79

Slide 79 text

Prototypal Scoping Fixed Demo

Slide 80

Slide 80 text

Prototypal Scoping • Reusable functions • Perfect when sharing data is desired • Shared data is problematic when reusing directive • Fixing shared data issues duplicates code • Assumptions about parent scopes

Slide 81

Slide 81 text

Requiring Directive Controllers

Slide 82

Slide 82 text

Requiring Directive Controllers Export an API from a parent directive

Slide 83

Slide 83 text

Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery', scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; });

Slide 84

Slide 84 text

Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery', scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; });

Slide 85

Slide 85 text

Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery', scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; }); Directive name

Slide 86

Slide 86 text

Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery', scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; }); Parent

Slide 87

Slide 87 text

Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery', scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; });

Slide 88

Slide 88 text

Requiring Directive Controllers app.directive('imageList', function() { return { require: '^imageGallery', scope: true, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { ctrl.setMainImage(scope.images[0]); scope.ctrl = ctrl; } }; });

Slide 89

Slide 89 text

Requiring Directive Controllers app.directive('imageGallery', function() { return { scope: true, templateUrl: 'templates/image-gallery.html', controller: function() { // ... } }; });

Slide 90

Slide 90 text

Requiring Directive Controllers app.directive('imageGallery', function() { return { scope: true, templateUrl: 'templates/image-gallery.html', controller: function() { // ... } }; });

Slide 91

Slide 91 text

controller: function() { this.mainImage = {}; this.imageFavorites = []; this.setMainImage = function(image) { this.mainImage.src = image.src; }; this.favorite = function(image) { if (!image.favorited) { image.favorited = true; this.imageFavorites.push(image); } }; this.unfavorite = function(image) { var i = this.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; this.imageFavorites.splice(i, 1); } }; }

Slide 92

Slide 92 text

controller: function() { this.mainImage = {}; this.imageFavorites = []; this.setMainImage = function(image) { this.mainImage.src = image.src; }; this.favorite = function(image) { if (!image.favorited) { image.favorited = true; this.imageFavorites.push(image); } }; this.unfavorite = function(image) { var i = this.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; this.imageFavorites.splice(i, 1); } }; }

Slide 93

Slide 93 text

controller: function() { this.mainImage = {}; this.imageFavorites = []; this.setMainImage = function(image) { this.mainImage.src = image.src; }; this.favorite = function(image) { if (!image.favorited) { image.favorited = true; this.imageFavorites.push(image); } }; this.unfavorite = function(image) { var i = this.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; this.imageFavorites.splice(i, 1); } }; }

Slide 94

Slide 94 text

Requiring Directive Controllers app.directive('mainImage', function() { return { require: '^imageGallery', // ... link: function(scope, el, attrs, ctrl) { scope.ctrl = ctrl; } } }).directive('imageFavorites', function() { return { require: '^imageGallery', // ... link: function(scope, el, attrs, ctrl) { scope.ctrl = ctrl; } }; });

Slide 95

Slide 95 text

Requiring Directive Controllers <h3>Main Image</h3> <img width="160" ng-src="{{ctrl.mainImage.src}}">

Slide 96

Slide 96 text

Requiring Directive Controllers <h3>Main Image</h3> <img width="160" ng-src="{{ctrl.mainImage.src}}">

Slide 97

Slide 97 text

Requiring Directive Controllers <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="ctrl.favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="ctrl.setMainImage(image)"> Set Main Image </button> </div>

Slide 98

Slide 98 text

Requiring Directive Controllers <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="ctrl.favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="ctrl.setMainImage(image)"> Set Main Image </button> </div>

Slide 99

Slide 99 text

Requiring Directive Controllers <h3>Favorites</h3> <h4>Favorited: {{ctrl.imageFavorites.length}}</h4> <ul> <li ng-repeat="image in ctrl.imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="ctrl.unfavorite(image)"> Unfavorite </button> </p> </li> </ul>

Slide 100

Slide 100 text

Requiring Directive Controllers <h3>Favorites</h3> <h4>Favorited: {{ctrl.imageFavorites.length}}</h4> <ul> <li ng-repeat="image in ctrl.imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="ctrl.unfavorite(image)"> Unfavorite </button> </p> </li> </ul>

Slide 101

Slide 101 text

Requiring Directive Controllers Demo

Slide 102

Slide 102 text

Requiring Directive Controllers • Consolidate behavior into a controller API • Keep own copy of data instead of sharing • Higher coupling — child directives depend upon parent directive • Still need a parent controller to scope own images (cat versus nature)

Slide 103

Slide 103 text

Isolate Scopes

Slide 104

Slide 104 text

Isolate Scopes • Directive scope is isolated from other scopes – No parent scope to inherit from – Can’t access other scopes “easily” • Use object literal for scope definition scope: { age: '@age', name: '=', selectHobby: '&onSelectHobby' }

Slide 105

Slide 105 text

Isolate Scopes scope: { name: '=', age: '@age', selectHobby: '&onSelectHobby' }

Slide 106

Slide 106 text

scope: { name: '=', age: '@age', selectHobby: '&onSelectHobby' } Isolate Scopes Explicitly specify HTML attribute

Slide 107

Slide 107 text

scope: { name: '=', age: '@age', selectHobby: '&onSelectHobby' } Isolate Scopes Implicit — use the scope key for HTML attribute

Slide 108

Slide 108 text

Isolate Scopes scope: { name: '=', age: '@age', selectHobby: '&onSelectHobby' } Bidirectional data binding Use property scopeName from current scope

Slide 109

Slide 109 text

Isolate Scopes scope: { name: '=', age: '@age', selectHobby: '&onSelectHobby' } Attribute/string binding; static. Use literal string "28"

Slide 110

Slide 110 text

Isolate Scopes scope: { name: '=', age: '@age', selectHobby: '&onSelectHobby' } Execute code in a “parent” scope (i.e. a callback) Function call with any parameter names. Uses function printHobby on parent scope (not the directive scope).

Slide 111

Slide 111 text

Isolate Scopes app.directive('nameTag', function() { return { scope: { age: '@age', name: '=', selectHobby: '&onSelectHobby' }, templateUrl: 'name-tag.html', link: function(scope, el, attrs) { scope.hobbies = ['program', 'read', 'play guitar']; scope.age = attrs.age; } }; });

Slide 112

Slide 112 text

Isolate Scopes <p>Hi, my name is {{name}}. I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul>

Slide 113

Slide 113 text

Isolate Scopes <p>Hi, my name is {{name}}. I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul>

Slide 114

Slide 114 text

Isolate Scopes <p>Hi, my name is {{name}}. I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul>

Slide 115

Slide 115 text

Isolate Scopes <p>Hi, my name is {{name}}. I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul>

Slide 116

Slide 116 text

Isolate Scopes <p>Hi, my name is {{name}}. I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul>

Slide 117

Slide 117 text

<p>Hi, my name is {{name}}. I am {{age}} years old.</p> <p><input type="text" ng-model="name"></p> <ul> <li ng-repeat="hobby in hobbies"> <a href="javascript:void(0)" ng-click="selectHobby({ hobby: hobby })"> {{hobby}} </a> </li> </ul> Isolate Scopes Invocations require “named arguments” via object literal.

Slide 118

Slide 118 text

Isolate Scopes Apply to the image gallery now

Slide 119

Slide 119 text

Isolate Scopes

Slide 120

Slide 120 text

Isolate Scopes

Slide 121

Slide 121 text

Isolate Scopes app.controller('ImageCtrl', function($scope) { $scope.catImages = [ createImage('/images/cat-1.jpg', 'Cat 1'), createImage('/images/cat-2.jpg', 'Cat 2'), createImage('/images/cat-3.jpg', 'Cat 3'), createImage('/images/cat-4.jpg', 'Cat 4') ]; $scope.natureImages = [ createImage('/images/nature-1.jpg', 'Nature 1'), createImage('/images/nature-2.jpg', 'Nature 2'), createImage('/images/nature-3.jpg', 'Nature 3'), createImage('/images/nature-4.jpg', 'Nature 4') ]; });

Slide 122

Slide 122 text

Isolate Scopes app.directive('imageGallery', function() { return { scope: { images: '=' }, templateUrl: 'templates/image-gallery.html', link: function(scope) { // ... } }; });

Slide 123

Slide 123 text

Isolate Scopes app.directive('imageGallery', function() { return { scope: { images: '=' }, templateUrl: 'templates/image-gallery.html', link: function(scope) { // ... } }; });

Slide 124

Slide 124 text

Isolate Scopes link: function(scope) { scope.mainImage = {}; scope.imageFavorites = []; scope.setMain = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { scope.imageFavorites.push(image); }; scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { scope.imageFavorites.splice(i, 1); } }; }

Slide 125

Slide 125 text

Isolate Scopes link: function(scope) { scope.mainImage = {}; scope.imageFavorites = []; scope.setMain = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { scope.imageFavorites.push(image); }; scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { scope.imageFavorites.splice(i, 1); } }; }

Slide 126

Slide 126 text

Isolate Scopes link: function(scope) { scope.mainImage = {}; scope.imageFavorites = []; scope.setMain = function(image) { scope.mainImage.src = image.src; }; scope.favorite = function(image) { scope.imageFavorites.push(image); }; scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { scope.imageFavorites.splice(i, 1); } }; }

Slide 127

Slide 127 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 128

Slide 128 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 129

Slide 129 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 130

Slide 130 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 131

Slide 131 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 132

Slide 132 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 133

Slide 133 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 134

Slide 134 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 135

Slide 135 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 136

Slide 136 text

Isolate Scopes <main-image image="mainImage"></main-image> <image-list images="images" on-set-main="setMain(image)" on-favorite="favorite(image)"></image-list> <image-favorites images="imageFavorites" on-unfavorite="unfavorite(image)"></image-favorites>

Slide 137

Slide 137 text

Isolate Scopes app.directive('mainImage', function() { return { scope: { mainImage: '=image' }, templateUrl: 'templates/main-image.html' } });

Slide 138

Slide 138 text

Isolate Scopes app.directive('mainImage', function() { return { scope: { mainImage: '=image' }, templateUrl: 'templates/main-image.html' } });

Slide 139

Slide 139 text

Isolate Scopes <h3>Main Image</h3> <img width="160" ng-src="{{mainImage.src}}">

Slide 140

Slide 140 text

Isolate Scopes <h3>Main Image</h3> <img width="160" ng-src="{{mainImage.src}}">

Slide 141

Slide 141 text

Isolate Scopes app.directive('imageFavorites', function() { return { scope: { imageFavorites: '=images', triggerUnfavorite: '&onUnfavorite' }, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { image.favorited = false; scope.triggerUnfavorite({ image: image }); }; } }; });

Slide 142

Slide 142 text

Isolate Scopes app.directive('imageFavorites', function() { return { scope: { imageFavorites: '=images', triggerUnfavorite: '&onUnfavorite' }, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { image.favorited = false; scope.triggerUnfavorite({ image: image }); }; } }; });

Slide 143

Slide 143 text

Isolate Scopes app.directive('imageFavorites', function() { return { scope: { imageFavorites: '=images', triggerUnfavorite: '&onUnfavorite' }, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.unfavorite = function(image) { image.favorited = false; scope.triggerUnfavorite({ image: image }); }; } }; });

Slide 144

Slide 144 text

Isolate Scopes <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul>

Slide 145

Slide 145 text

Isolate Scopes <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul>

Slide 146

Slide 146 text

Isolate Scopes <h3>Favorites</h3> <h4>Favorited: {{imageFavorites.length}}</h4> <ul> <li ng-repeat="image in imageFavorites"> <p> <img ng-src="{{image.src}}" width="40"> {{image.text}} <button ng-click="unfavorite(image)"> Unfavorite </button> </p> </li> </ul>

Slide 147

Slide 147 text

app.directive('imageList', function() { return { scope: { images: '=', setMainImage: '&onSetMain', triggerFavorite: '&onFavorite' }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage({ image: scope.images[0] }); scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.triggerFavorite({ image: image }); } }; } }; });

Slide 148

Slide 148 text

app.directive('imageList', function() { return { scope: { images: '=', setMainImage: '&onSetMain', triggerFavorite: '&onFavorite' }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage({ image: scope.images[0] }); scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.triggerFavorite({ image: image }); } }; } }; });

Slide 149

Slide 149 text

app.directive('imageList', function() { return { scope: { images: '=', setMainImage: '&onSetMain', triggerFavorite: '&onFavorite' }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage({ image: scope.images[0] }); scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.triggerFavorite({ image: image }); } }; } }; });

Slide 150

Slide 150 text

app.directive('imageList', function() { return { scope: { images: '=', setMainImage: '&onSetMain', triggerFavorite: '&onFavorite' }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage({ image: scope.images[0] }); scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.triggerFavorite({ image: image }); } }; } }; });

Slide 151

Slide 151 text

Isolate Scopes <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage({ image: image })"> Set Main Image </button> </div>

Slide 152

Slide 152 text

Isolate Scopes <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage({ image: image })"> Set Main Image </button> </div>

Slide 153

Slide 153 text

Isolate Scopes <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage({ image: image })"> Set Main Image </button> </div>

Slide 154

Slide 154 text

Isolate Scopes Demo

Slide 155

Slide 155 text

Isolate Scopes • Black box a directive to be self-contained and reusable • Think in terms of small pieces • Establish well-defined interface/API through HTML attributes • Juggle multiple points of state • More markup

Slide 156

Slide 156 text

Events

Slide 157

Slide 157 text

Events • Built in eventing with $emit, $broadcast, and $on methods of $scope. • Use events like a message bus to transfer data • Events travel vertically through scopes – Use parent scope event delegation for sibling directive communication

Slide 158

Slide 158 text

Events $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); // In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); });

Slide 159

Slide 159 text

$scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); // In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Events $emit sends events up to parent scopes.

Slide 160

Slide 160 text

$scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); // In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Events $broadcast sends events down to child scopes.

Slide 161

Slide 161 text

$scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); // In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Events $on listens for $emit and $broadcast events and triggers the provided callbacks.

Slide 162

Slide 162 text

// In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events The first parameter is an event object like DOM events.

Slide 163

Slide 163 text

// In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); $scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events Remaining parameters are the remaining arguments supplied to $emit and $broadcast.

Slide 164

Slide 164 text

$scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events // In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Stick to a naming convention like verb:noun.

Slide 165

Slide 165 text

$scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events // In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Verb

Slide 166

Slide 166 text

$scope.$emit('select:greeting', 'hello world'); $scope.$broadcast('add:item', 42); Events // In a child directive var items = []; $scope.$on('add:item', function(e, n) { items.push(n); }); // In a parent directive $scope.$on('select:greeting', function(e, greeting) { console.log(greeting.toUpperCase()); }); Noun

Slide 167

Slide 167 text

Events Apply to the image gallery now

Slide 168

Slide 168 text

Events <main-image></main-image> <image-list images="images"></image-list> <image-favorites></image-favorites>

Slide 169

Slide 169 text

Events <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div>

Slide 170

Slide 170 text

Events <h3>Images</h3> <div class="image-info" ng-repeat="image in images"> <div ng-click="favorite(image)"> <img ng-src="{{image.src}}"> </div> <p>{{image.text}}</p> <button ng-click="setMainImage(image)"> Set Main Image </button> </div>

Slide 171

Slide 171 text

app.directive('imageList', function() { return { scope: { images: '=', }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage = function(image) { scope.$emit('delegate:set:mainImage', image); }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.$emit('delegate:favorite:image', image); } }; scope.setMainImage(scope.images[0]); } }; });

Slide 172

Slide 172 text

app.directive('imageList', function() { return { scope: { images: '=', }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage = function(image) { scope.$emit('delegate:set:mainImage', image); }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.$emit('delegate:favorite:image', image); } }; scope.setMainImage(scope.images[0]); } }; });

Slide 173

Slide 173 text

app.directive('imageList', function() { return { scope: { images: '=', }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage = function(image) { scope.$emit('delegate:set:mainImage', image); }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.$emit('delegate:favorite:image', image); } }; scope.setMainImage(scope.images[0]); } }; });

Slide 174

Slide 174 text

app.directive('imageList', function() { return { scope: { images: '=', }, templateUrl: 'templates/image-list.html', link: function(scope, el, attrs, ctrl) { scope.setMainImage = function(image) { scope.$emit('delegate:set:mainImage', image); }; scope.favorite = function(image) { if (!image.favorited) { image.favorited = true; scope.$emit('delegate:favorite:image', image); } }; scope.setMainImage(scope.images[0]); } }; });

Slide 175

Slide 175 text

Events app.directive('imageGallery', function() { return { scope: { images: '=' }, templateUrl: 'templates/image-gallery.html', link: function(scope) { scope.$on('delegate:set:mainImage', function(e, image) { scope.$broadcast('set:mainImage', image); }); scope.$on('delegate:favorite:image', function(e, image) { scope.$broadcast('favorite:image', image); }); } }; });

Slide 176

Slide 176 text

Events app.directive('imageGallery', function() { return { scope: { images: '=' }, templateUrl: 'templates/image-gallery.html', link: function(scope) { scope.$on('delegate:set:mainImage', function(e, image) { scope.$broadcast('set:mainImage', image); }); scope.$on('delegate:favorite:image', function(e, image) { scope.$broadcast('favorite:image', image); }); } }; });

Slide 177

Slide 177 text

Events app.directive('mainImage', function() { return { scope: {}, templateUrl: 'templates/main-image.html', link: function(scope) { scope.mainImage = {}; scope.$on('set:mainImage', function(e, image) { scope.mainImage.src = image.src; }); } } });

Slide 178

Slide 178 text

Events app.directive('mainImage', function() { return { scope: {}, templateUrl: 'templates/main-image.html', link: function(scope) { scope.mainImage = {}; scope.$on('set:mainImage', function(e, image) { scope.mainImage.src = image.src; }); } } });

Slide 179

Slide 179 text

Events app.directive('mainImage', function() { return { scope: {}, templateUrl: 'templates/main-image.html', link: function(scope) { scope.mainImage = {}; scope.$on('set:mainImage', function(e, image) { scope.mainImage.src = image.src; }); } } });

Slide 180

Slide 180 text

Events app.directive('mainImage', function() { return { scope: {}, templateUrl: 'templates/main-image.html', link: function(scope) { scope.mainImage = {}; scope.$on('set:mainImage', function(e, image) { scope.mainImage.src = image.src; }); } } });

Slide 181

Slide 181 text

app.directive('imageFavorites', function() { return { scope: {}, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.imageFavorites = []; scope.$on('favorite:image', function(e, image) { scope.imageFavorites.push(image); }); scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });

Slide 182

Slide 182 text

app.directive('imageFavorites', function() { return { scope: {}, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.imageFavorites = []; scope.$on('favorite:image', function(e, image) { scope.imageFavorites.push(image); }); scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });

Slide 183

Slide 183 text

app.directive('imageFavorites', function() { return { scope: {}, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.imageFavorites = []; scope.$on('favorite:image', function(e, image) { scope.imageFavorites.push(image); }); scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });

Slide 184

Slide 184 text

app.directive('imageFavorites', function() { return { scope: {}, templateUrl: 'templates/image-favorites.html', link: function(scope) { scope.imageFavorites = []; scope.$on('favorite:image', function(e, image) { scope.imageFavorites.push(image); }); scope.unfavorite = function(image) { var i = scope.imageFavorites.indexOf(image); if (i > -1) { image.favorited = false; scope.imageFavorites.splice(i, 1); } }; } }; });

Slide 185

Slide 185 text

Demo Events

Slide 186

Slide 186 text

Events • Decouple directives • Isolate state more easily • Cleaner markup • Indirection of data flow (who responds to an event?) • Have to remember and duplicate event names (may hurt directive reuse)

Slide 187

Slide 187 text

Upshot

Slide 188

Slide 188 text

Upshot Refactoring

Slide 189

Slide 189 text

Refactoring • Easier to change or update app • Don’t have to touch entire app, just an individual directive – Touching multiple pieces may mean better SOC needed – SOC is evolutionary for the lifetime of the app

Slide 190

Slide 190 text

Refactoring • Remove count • Remove image text • Sort images by text Alter image gallery favorites Demo

Slide 191

Slide 191 text

Refactoring Alter image gallery favorites <h3>Favorites</h3> <ul> <li ng-repeat="image in imageFavorites | orderBy: 'text'"> <p> <img ng-src="{{image.src}}" width="40"> </p> <button ng-clik="unfavorite(image)"> Unfavorite </button> </li> </ul>

Slide 192

Slide 192 text

<h3>Favorites</h3> <ul> <li ng-repeat="image in imageFavorites | orderBy: 'text'"> <p> <img ng-src="{{image.src}}" width="40"> </p> <button ng-clik="unfavorite(image)"> Unfavorite </button> </li> </ul> Refactoring Alter image gallery favorites Favorites count gone

Slide 193

Slide 193 text

Refactoring Alter image gallery favorites <h3>Favorites</h3> <ul> <li ng-repeat="image in imageFavorites | orderBy: 'text'"> <p> <img ng-src="{{image.src}}" width="40"> </p> <button ng-clik="unfavorite(image)"> Unfavorite </button> </li> </ul> Image text gone

Slide 194

Slide 194 text

Refactoring Alter image gallery favorites <h3>Favorites</h3> <ul> <li ng-repeat="image in imageFavorites | orderBy: 'text'"> <p> <img ng-src="{{image.src}}" width="40"> </p> <button ng-clik="unfavorite(image)"> Unfavorite </button> </li> </ul>

Slide 195

Slide 195 text

Composition is a critical tool in software development.

Slide 196

Slide 196 text

Thanks! jfairbank @elpapapollo blog.jeremyfairbank.com Demo and code github.com/jfairbank/orchestrating-apps-angular Slides: speakerdeck.com/jfairbank/connectjs-orchestrating-apps-by-composing-angular-directives