Slide 1

Slide 1 text

Understanding Components in Angular 1.5 Jeremy Wilken
 @gnomeontherun

Slide 2

Slide 2 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun About Me 2 JEREMY WILKEN SENIOR SOFTWARE ENGINEER @ TERADATA • Lives in Austin, TX • Author of Angular 2 in Action, Ionic in Action • Instructor for Angular/Frontend Technologies • Avid home brewer • Twitter/Github @gnomeontherun https://manning.com/books/angular-2-in-action https://manning.com/books/ionic-in-action

Slide 3

Slide 3 text

What are Components? A better way to build apps

Slide 4

Slide 4 text

Components are everywhere

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

component (noun): a part or element of a larger whole, especially a part of a machine or vehicle - dictionary “ 6

Slide 7

Slide 7 text

HTML is a language of components Web applications are essentially the DOM come to life.

Slide 8

Slide 8 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Why Components 8

Slide 9

Slide 9 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Why Components 9 Encapsulation Acts as a single unit, easy to reason about

Slide 10

Slide 10 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Why Components 10 Encapsulation Isolation Acts as a single unit, easy to reason about Keep internal logic hidden from user

Slide 11

Slide 11 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Why Components 11 Encapsulation Isolation Declarative Acts as a single unit, easy to reason about Markup expresses application structure Keep internal logic hidden from user

Slide 12

Slide 12 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Why Components 12 Encapsulation Isolation Well Defined API Declarative Acts as a single unit, easy to reason about Expose component options consistently Markup expresses application structure Keep internal logic hidden from user

Slide 13

Slide 13 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Why Components 13 Encapsulation Isolation Well Defined API Like Native Declarative Acts as a single unit, easy to reason about Expose component options consistently Markup expresses application structure Keep internal logic hidden from user Functions like normal HTML elements

Slide 14

Slide 14 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Why Components 14 Encapsulation Isolation Well Defined API Like Native Declarative Angular 2 Acts as a single unit, easy to reason about Expose component options consistently Markup expresses application structure Keep internal logic hidden from user Functions like normal HTML elements Follows patterns of Angular 2

Slide 15

Slide 15 text

The road to components How apps were built over time

Slide 16

Slide 16 text

16 The road to components Evolution of Angular 1 App Design 1 Tightly coupled templates and controllers by embedding ngController directly into markup. Difficult to maintain, test, and isolate. Put it in the template! ngController 2 Coupling only increased by using $scope, often leading to controllers having inheritance. Testable, but often hard to maintain. $scope is where its all at! $scope

Slide 17

Slide 17 text

17 4 Isolates the data by storing it on a property of $scope that the template cannot access, further reducing coupling, but didn’t bind data to directives controllers. Isolate model from $scope! controllerAs 5 Instead of coupling to a state, directives can glue the template and controllers together, but directives can be tricky to write, have many options, and use $scope. Make component like directives! directives 3 Less coupling by mapping templates and controllers in a state definition, limiting inheritance but still using $scope. Link controllers and templates in states! ui-router

Slide 18

Slide 18 text

18 7 Provide sane defaults and simpler syntax for creating components, keeping the focus on smaller components. First class support for components! components 6 Binds the inputs from a directive directly to the controller instead of $scope, but syntax is clunky and easy to over complicate the directive. Make it possible to remove $scope in directives! bindToController

Slide 19

Slide 19 text

Using Components How they are declared and consumed

Slide 20

Slide 20 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Consuming a component 20

Slide 21

Slide 21 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Syntax to declare a component 21 angular.module('App', []) .component('myComponent', { bindings: { title: '=' }, templateUrl: 'my-component.html', controller: function(MyService) { this.data = MyService.load(); }, });

Slide 22

Slide 22 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Component Lifecycle Events 22 1 3 2 4

Slide 23

Slide 23 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Component Lifecycle Events 23 1 $onInit() Runs after the controller and bindings are initialized. Use to do any initialization for your component. 3 2 4

Slide 24

Slide 24 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Component Lifecycle Events 24 1 $onInit() Runs after the controller and bindings are initialized. Use to do any initialization for your component. 3 2 $postLink() 4 Runs after the element and all children have been linked. If you do any DOM manipulation, this is the place, but be wary of async loading of templates.

Slide 25

Slide 25 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Component Lifecycle Events 25 1 $onInit() Runs after the controller and bindings are initialized. Use to do any initialization for your component. 3 $onChanges() Runs anytime a one-way data binding is updated. Use it to trigger updates inside of a component. 2 $postLink() 4 Runs after the element and all children have been linked. If you do any DOM manipulation, this is the place, but be wary of async loading of templates.

Slide 26

Slide 26 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Component Lifecycle Events 26 1 $onInit() Runs after the controller and bindings are initialized. Use to do any initialization for your component. $onDestroy() 3 $onChanges() Runs anytime a one-way data binding is updated. Use it to trigger updates inside of a component. 2 $postLink() Runs right before the controller and scope is destroyed. If you setup event listeners or watchers, clean them up here. 4 Runs after the element and all children have been linked. If you do any DOM manipulation, this is the place, but be wary of async loading of templates.

Slide 27

Slide 27 text

Types of Components There are different roles components can fill.

Slide 28

Slide 28 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Types of Components 28

Slide 29

Slide 29 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Types of Components 29 Houses the primary app framework. Should be as simple as possible. Primarily a template to display app shell. Try to avoid a controller and bindings. App

Slide 30

Slide 30 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Types of Components 30 Houses the primary app framework. Should be as simple as possible. Primarily a template to display app shell. Try to avoid a controller and bindings. Components that are loaded specifically to manage a single route, like a view. Can bind additional metadata into the component based on route url params. Should focus on setting up the route and rely on other components to render. App Routing

Slide 31

Slide 31 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Types of Components 31 Houses the primary app framework. Should be as simple as possible. Primarily a template to display app shell. Try to avoid a controller and bindings. Components that are loaded specifically to manage a single route, like a view. Can bind additional metadata into the component based on route url params. Should focus on setting up the route and rely on other components to render. Components that manage business logic, load data, and other stateful tasks. Focus on using services and maintaining data, not on presentation. Should render other components for interface. App Routing Stateful

Slide 32

Slide 32 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Types of Components 32 Houses the primary app framework. Should be as simple as possible. Primarily a template to display app shell. Try to avoid a controller and bindings. Components that are loaded specifically to manage a single route, like a view. Can bind additional metadata into the component based on route url params. Should focus on setting up the route and rely on other components to render. Components that manage business logic, load data, and other stateful tasks. Focus on using services and maintaining data, not on presentation. Should render other components for interface. Components that are primarily UI interface elements. Should be extremely decoupled from other components. May accept configurations to modify component behavior. App Routing Stateful Stateless

Slide 33

Slide 33 text

Component Trees Apps can be a tree of components that mirror the DOM

Slide 34

Slide 34 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Apps as Component Trees 34

Slide 35

Slide 35 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Apps as Component Trees 35 App

Slide 36

Slide 36 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Apps as Component Trees 36 App Routes

Slide 37

Slide 37 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Apps as Component Trees 37 App Routes Stateful

Slide 38

Slide 38 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Apps as Component Trees 38 App Routes Stateful Stateless

Slide 39

Slide 39 text

Components vs Directives Important distinctions and uses

Slide 40

Slide 40 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 40 Directives Components

Slide 41

Slide 41 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 41 Use to modify existing elements via attributes Directives Components

Slide 42

Slide 42 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 42 Use when compile or link is needed Use to modify existing elements via attributes Directives Components

Slide 43

Slide 43 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 43 Use for modifying the DOM Use when compile or link is needed Use to modify existing elements via attributes Directives Components

Slide 44

Slide 44 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 44 Use for modifying the DOM Use when compile or link is needed Use to modify existing elements via attributes Use when advanced configuration required Directives Components

Slide 45

Slide 45 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 45 Use when creating new elements Use for modifying the DOM Use when compile or link is needed Use to modify existing elements via attributes Use when advanced configuration required Directives Components

Slide 46

Slide 46 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 46 Use when a controller or template is needed Use when creating new elements Use for modifying the DOM Use when compile or link is needed Use to modify existing elements via attributes Use when advanced configuration required Directives Components

Slide 47

Slide 47 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 47 Use for creating DOM elements Use when a controller or template is needed Use when creating new elements Use for modifying the DOM Use when compile or link is needed Use to modify existing elements via attributes Use when advanced configuration required Directives Components

Slide 48

Slide 48 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun When to use a component or directive 48 Use for creating DOM elements Use when a controller or template is needed Use when creating new elements Use with basic configuration Use for modifying the DOM Use when compile or link is needed Use to modify existing elements via attributes Use when advanced configuration required Directives Components

Slide 49

Slide 49 text

Let’s Build Some Stock tracking app with components

Slide 50

Slide 50 text

https://github.com/gnomeontherun/
 angular-1.5-components 50

Slide 51

Slide 51 text

Compared to Angular 2 Making Angular 1 more like Angular 2

Slide 52

Slide 52 text

52 Components in Angular 1 and 2 Angular 1 Angular 2 angular.module('App', []) .component('myComponent', { bindings: { title: '=' }, templateUrl: 'my-component.html', controller: function(MyService) { this.data = MyService.load(); }, }); @Component({ selector: 'my-component', templateUrl: 'my-component.html' }) export class MyComponent { @Input() title: string; constructor(service: MyService) { this.data = service.load(); } }

Slide 53

Slide 53 text

Understanding Components in Angular 1.5 Jeremy Wilken @gnomeontherun Additional Resources 53 • Documentation (https://docs.angularjs.org/guide/component) • ng-metadata (https://github.com/ngParty/ng-metadata) • Exploring the Angular 1.5 .component() method
 (https://toddmotto.com/exploring-the-angular-1-5-component-method/) • Exploring Angular 1.5 Lifecycle Hooks (http://blog.thoughtram.io/angularjs/ 2016/03/29/exploring-angular-1.5-lifecycle-hooks.html)

Slide 54

Slide 54 text

Thank You We’re hiring!
 austin.teradata.com @gnomeontherun 54