Getting Started with Angular 2 and TypeScript

59
Getting Started with Angular 2 and TypeScript Spencer Schneidenbach Ryvit/GadellNet @schneidenbach

Transcript of Getting Started with Angular 2 and TypeScript

Page 1: Getting Started with Angular 2 and TypeScript

Getting Started with Angular 2 and

TypeScriptSpencer Schneidenbach

Ryvit/GadellNet

@schneidenbach

Page 2: Getting Started with Angular 2 and TypeScript

About MePlatform Architect at RyvitConsultantMicrosoft MVP

schneids.net

@schneidenbach

Page 3: Getting Started with Angular 2 and TypeScript
Page 4: Getting Started with Angular 2 and TypeScript

Setting the stage

(yes, I know React is not a framework!)

Page 5: Getting Started with Angular 2 and TypeScript

Why Angular 2?

Page 6: Getting Started with Angular 2 and TypeScript

Why TypeScript?

Page 7: Getting Started with Angular 2 and TypeScript

<3

Page 8: Getting Started with Angular 2 and TypeScript

Main reason why TypeScript is great• “TypeScript doesn’t try to NOT be JavaScript – it just tries to make

JavaScript better” –Me

Page 9: Getting Started with Angular 2 and TypeScript

So, let’s go over some TypeScript basics!

Page 10: Getting Started with Angular 2 and TypeScript

Interfaces

Page 11: Getting Started with Angular 2 and TypeScript

Interfaces

Page 12: Getting Started with Angular 2 and TypeScript

Interfaces“If it walks like a duck and quacks like a duck… it’s a duck”

Page 13: Getting Started with Angular 2 and TypeScript

Classes

Page 14: Getting Started with Angular 2 and TypeScript

Using classes

Page 15: Getting Started with Angular 2 and TypeScript

Constructor/property shorthand

Page 16: Getting Started with Angular 2 and TypeScript

Decorators• Like attributes in C#/VB.NET

Page 17: Getting Started with Angular 2 and TypeScript

import/export• Like using statements in C#/VB.NET• import statements allow us to reference code from other TypeScript

files• export simply means public

Page 18: Getting Started with Angular 2 and TypeScript

Enough TypeScript! Angular 2 Time!

Page 19: Getting Started with Angular 2 and TypeScript

History lesson• Angular 1 – started in 2009• Hugely popular – won the initial “SPA war”

Page 20: Getting Started with Angular 2 and TypeScript

Enter Angular 2

Page 21: Getting Started with Angular 2 and TypeScript

Let’s jump right in!

Page 22: Getting Started with Angular 2 and TypeScript

Modules

Page 23: Getting Started with Angular 2 and TypeScript

Contrived exampleAccounting

Accounts PayableAccounts ReceivablePayroll

ITSystems supportDevelopment

CustomersSalesAccount managementSupport

Page 24: Getting Started with Angular 2 and TypeScript

Running your app• Import your root NgModule• Bootstrap it!

Page 25: Getting Started with Angular 2 and TypeScript

Components

Page 26: Getting Started with Angular 2 and TypeScript

Declaring a component• Create your class• Import the Component decorator• Add a template and a selector

Page 27: Getting Started with Angular 2 and TypeScript

Templates in your component

@Component({selector: 'my-app',template: '<h1>This is some HTML</h1>'

})export class AppComponent {}

Page 28: Getting Started with Angular 2 and TypeScript

Styling a component• You can add CSS directly to a component! Demo

Page 29: Getting Started with Angular 2 and TypeScript

Components

Favor many smaller components over fewer bigger ones.

Page 30: Getting Started with Angular 2 and TypeScript

ngModelEvent binding

Property bindingInterpolation expressions

Binding component to template

Page 31: Getting Started with Angular 2 and TypeScript

Interpolated expressions

Page 32: Getting Started with Angular 2 and TypeScript

Property binding

Page 33: Getting Started with Angular 2 and TypeScript

Property binding• Property binding means that all sorts of previous built-in directives in

Angular 1 go away

Ng1 Directive Functionality Replaced with

ng-disabled Disables the control – used on inputs, selects, textareas

[disabled]=“expression”

ng-src/ng-href Replacement for src/href properties since interpolation didn’t play well with them

[src]=“expression”[href]=“expression”

ng-show/ng-hidden Hides/shows an element based on the truthiness of an expression

[hidden]=“expression”

Page 34: Getting Started with Angular 2 and TypeScript

Event binding• Bind directly to events on the DOM model using ()

Page 35: Getting Started with Angular 2 and TypeScript

Event binding, cont’d• Property binding means that all sorts of previous built-in directives in

Angular 1 go away

Ng1 directive Functionality Replaced with

ng-click Binds an expression to the click event (click)=“doSomething()”

ng-change Binds an expression to the input changed event (change)=“prop = $event”(ngModelChange)=“”

ng-dblclick Binds an expression to the double click event (dblclick)=“onDoubleClick()”

ng-mouseover …mouse over event

ng-mousemove …mouse move event

…you get the idea

Page 36: Getting Started with Angular 2 and TypeScript

ngModel• Allows two-way binding • Follows unidirectional data flow concepts• Demo

Page 37: Getting Started with Angular 2 and TypeScript

Child components• A child component is a component that exists within a component• I know, pretty crazy right?

Page 38: Getting Started with Angular 2 and TypeScript

Step 1: Declare your component

Page 39: Getting Started with Angular 2 and TypeScript

Step 2: Import it to your containing module

import { ChildComponent } from "./child.component";

@NgModule({declarations: [AppComponent]

})export class AppModule {

}

Page 40: Getting Started with Angular 2 and TypeScript

Step 3: Registerimport { ChildComponent } from "./child.component";

@NgModule({declarations: [AppComponent, ChildComponent]

})export class AppModule {

}

Page 41: Getting Started with Angular 2 and TypeScript

Step 4: Add child selector to markup

Page 42: Getting Started with Angular 2 and TypeScript

But how do components talk??

Inputs and Outputs

Page 43: Getting Started with Angular 2 and TypeScript

Let’s start with inputs

Page 44: Getting Started with Angular 2 and TypeScript

Step 1: Add the input property

Page 45: Getting Started with Angular 2 and TypeScript

Step 2: Bind the parent property to that property

Page 46: Getting Started with Angular 2 and TypeScript

Now Outputs and EventEmitter

Page 47: Getting Started with Angular 2 and TypeScript

Import and implement

Page 48: Getting Started with Angular 2 and TypeScript

Add subscribing function

Page 49: Getting Started with Angular 2 and TypeScript

Emit your event

Page 50: Getting Started with Angular 2 and TypeScript

Don’t mutate data in child components.

Page 51: Getting Started with Angular 2 and TypeScript

What are Services?• Services are best used to create, read, update and delete data• Enforces separation of concerns• Think of it like this: you don’t want your database in your view!

Page 52: Getting Started with Angular 2 and TypeScript

Services in Angular 1• In Angular 1, we had 5 different kinds of services:• Provider• Service• Factory• Value• Constants

• In Angular 2, we have 1 kind of service:• Service

• Which do you prefer?

Page 53: Getting Started with Angular 2 and TypeScript

Step 1 – Make class with data

Page 54: Getting Started with Angular 2 and TypeScript

Step 2 – Import @Injectable and decorate

Page 55: Getting Started with Angular 2 and TypeScript

Steps 3 and 4 – Import and register

Page 56: Getting Started with Angular 2 and TypeScript

Step 5 – Add as property and to constructor

Page 57: Getting Started with Angular 2 and TypeScript

Services

Components should know who can do something for them, not how to do it.

Page 58: Getting Started with Angular 2 and TypeScript

Things we couldn’t cover• Pipes• Forms• Routing• Http service• RxJS• So much more!

Page 59: Getting Started with Angular 2 and TypeScript

Thank you!Slides at angular2.schneids.net

schneids.net@schneidenbach