an afternoon with angular 2

42
AN AFTERNOON WITH ANGULAR 2 Michael Melusky - @mrjavascript Philly.NET Code Camp 2017.1 February 25, 2017

Upload: mike-melusky

Post on 22-Jan-2018

229 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: An afternoon with angular 2

AN AFTERNOON WITH ANGULAR 2

Michael Melusky - @mrjavascriptPhilly.NET Code Camp 2017.1

February 25, 2017

Page 2: An afternoon with angular 2

About Speaker

■ Michael Melusky

– Software Engineer for Audacious Inquiry in Baltimore MD

– Computer Science Professor

■ Penn State University

■ Franklin and Marshall College

– Hopeful 2017 Microsoft MVP ???

Page 3: An afternoon with angular 2

Topics

■ Introduction to Angular 2

■ TypeScript

■ Angular Components

■ Data Binding

– One Way (View -> Component and Component -> View)

– Two Way

■ Passing Data Between Components

■ Routing

■ Directives

■ Pipes and Filters

■ Services and HTTP Service

■ Firebase Integration

Page 4: An afternoon with angular 2

Angular 2

Page 5: An afternoon with angular 2

What is Angular 2?

■ JavaScript framework for creating dynamic web applications

■ Component Based (unlike MVC)

– Significantly different an Angular 1.x

■ Uses TypeScript

■ From Google, “Superheroic JavaScript MVW Framework!”

Page 6: An afternoon with angular 2

MVW ????

Page 7: An afternoon with angular 2

Pre-requisites

■ NPM (node package manager)

■ HTML5/CSS3

■ JavaScript / TypeScript

Page 8: An afternoon with angular 2

Node.js

Page 9: An afternoon with angular 2

Node.js

■ To verify Node installation:

– node – v

Page 10: An afternoon with angular 2

Angular CLI

■ To create an Angular 2 application:

– Install the Angular 2 CLI:

■ npm install –g angular-cli

■ ng new <app-name>

Page 11: An afternoon with angular 2

TypeScript

Page 12: An afternoon with angular 2

TypeScript Basics

■ Similar to JavaScript (strict-superset)

– Open source language developed by Microsoft

■ Adds static types

■ Adds class-based object-oriented programming

Page 13: An afternoon with angular 2

TypeScript Basics

■ In JavaScript:

– foo = “my string”

– foo = 25

■ Not allowed in TypeScript:

– foo = “my string”

– foo = 25

Page 14: An afternoon with angular 2

TypeScript Variable Declaration

■ foo:string = “hi there”

■ foo:number = 69

■ foo:boolean = false

■ foo:any

■ foo = 42

■ foo:string[]

Page 15: An afternoon with angular 2

TypeScript Classes

■ class Planet {

– moons:number = 50

– orbit {

■ console.log(“rotating around the sun”)

– }

■ }

■ pluto:Planet = new Planet()

Page 16: An afternoon with angular 2

TypeScript Constructor

■ class Planet {

– moons:number;

– constructor(numMoons:number) {

■ this.moons = numMoons;

– }

■ }

■ pluto:Planet = new Planet(1)

Page 17: An afternoon with angular 2

Angular 2 Components

Page 18: An afternoon with angular 2

Angular 2 Components

■ The main way to build elements and specify logic on a page

■ To create a component using the Angular CLI:

– Npm generate component <component-name>

■ A component is one type of an Angular 2 Directive (directives with templates)

Page 19: An afternoon with angular 2

Angular Components

Page 20: An afternoon with angular 2

Angular Components

Page 21: An afternoon with angular 2

ng-content Directive

■ Implement transclusion in Angular 2

■ Taking content from a text node or HTML

■ Injecting it into a template at an entry point

Page 22: An afternoon with angular 2

Data Binding

Page 23: An afternoon with angular 2

Angular 2 Data Binding

■ Bind from Component to Template

■ Bind from Template to Component

Page 24: An afternoon with angular 2

Angular Data Binding

Page 25: An afternoon with angular 2

One Way Binding

■ Data Into View

■ string interpolation

■ {{title}} (always resolves to a string)

■ property binding

■ <input [required]='expression'>

Page 26: An afternoon with angular 2

One Way Data Binding

■ Data Out of View

■ event binding

■ <button (click)='expression/function'>

■ component class can handle the event

Page 27: An afternoon with angular 2

Two Way Binding

■ <input [(ngModel)]='model/object'>

■ updates model in component class

■ updates any reference to model in view/template {{model}}

Page 28: An afternoon with angular 2

Communication Between Components

Page 29: An afternoon with angular 2

@Input Directive

■ Send data from the app to the home components (as example)

■ Custom property binding

Page 30: An afternoon with angular 2

@Output Directive

■ Receive Events occurring in Home Component in App Component

■ Custom Event Binding

Page 31: An afternoon with angular 2

Angular 2 Routing

Page 32: An afternoon with angular 2

Routing

■ For instance:

– /home -> use home component

– /directory -> use directory component

■ To accomplish this:

– Create a route structure

– Tell Angular where to load the view when a route is requested

Page 33: An afternoon with angular 2

Angular 2 Directives

Page 34: An afternoon with angular 2

Angular 2 Directives

■ - instructions which tell Angular to do something

■ - e.g. <router-outlet></router-outlet>

■ - Attribute –

– interact with element its sitting on to change its properties (ngClass, ngStyle)

■ - Structural –

– change structure of HTML code (ngif, ngFor)

Page 35: An afternoon with angular 2

Angular 2 Pipes

Page 36: An afternoon with angular 2

Angular 2 Pipes

■ - Used to be filters in Angular 1.x

■ - e.g. {{name | uppercase}}

■ - e.g. {{name | slice:1}}

■ - Filter pipe not in Angular 2

■ Can create custom pipes however!

Page 37: An afternoon with angular 2

Services

Page 38: An afternoon with angular 2

Services

■ Don’t Repeat Yourself!

■ Component 1:

– Does some stuff

– Connects to the database

■ Component 2

– Does a few things

– Connects to the database’

Page 39: An afternoon with angular 2

Firebase

Page 40: An afternoon with angular 2

Firebase

■ Google product

Page 41: An afternoon with angular 2

Thank You

Page 42: An afternoon with angular 2

Slide Notes

■ Code:

– Github.com/mrjavascript

■ Slides:

– Slideshare.com/mrjavascript

■ Thank you:

– “The Net Ninja” on YouTube for topic arrangements