Top Banner
ANGULAR 2 PRE-WORKSHOP Register in Slack “Open Web Uruguayhttp://owu.herokuapp.com/ Channel #jsconfuy-angular2 Channel Requirements Run $ ng new music $ cd music 1. NodeJS >= 4.0.0 2. Angular CLI $ npm install -g [email protected]
49

Angular 2 MVD workshop

Apr 15, 2017

Download

Internet

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Angular 2 MVD workshop

ANGULAR 2 PRE-WORKSHOP

Register in Slack “Open Web Uruguay” http://owu.herokuapp.com/

Channel #jsconfuy-angular2

Channel

Requirements Run

● $ ng new music

● $ cd music

1. NodeJS >= 4.0.0

2. Angular CLI

$ npm install -g [email protected]

Page 2: Angular 2 MVD workshop

WHERE

Antel, Guatemala 1075, Montevideo, Uruguay

Greetings,

ONLINE

Web: https://angular2-jsconf.herokuapp.com/

ANGULAR 2JSCONF WORKSHOPwith Iran Reyes & Santiago Ferreira

Page 3: Angular 2 MVD workshop

@iranfleitas @san650

Speakers

Page 4: Angular 2 MVD workshop

@matias_delgado @vlavella19

Collaborators

@sebasrodriguez

Page 5: Angular 2 MVD workshop
Page 6: Angular 2 MVD workshop
Page 7: Angular 2 MVD workshop

Table of Content

1. Angular 2

2. angular-cli

3. Base template

4. Components and binding

5. Consuming data

6. Routing

7. Services

Page 8: Angular 2 MVD workshop

ANGULAR 2

● Framework JS by Google (back to 7 years ago)

● Closer to Web Standards

● ES5 / ES6 / Typescript / Dart

● Faster, Semantically better, Easy, ...

Page 9: Angular 2 MVD workshop

MUSIC

Page 10: Angular 2 MVD workshop

ANGULAR-CLI

Page 11: Angular 2 MVD workshop

GUIDELINE

● Generators (Blueprints)

● Project structure based on conventions

● Test runner

● Development server

● Addons

● Deploys and other stuff…

Page 12: Angular 2 MVD workshop

ANGULAR-CLI

Page 13: Angular 2 MVD workshop

ANGULAR-CLI

● $ npm install -g [email protected]

● $ ng new music/

● $ cd music/

● $ ng server (en otra consola)

Page 14: Angular 2 MVD workshop

GUIDELINE

● Generators (Blueprints)

● Project structure based on conventions

● Test runner

● Development server

● Addons

● Deploys and other stuff…

Page 15: Angular 2 MVD workshop

BASE TEMPLATE

Page 16: Angular 2 MVD workshop

BASE TEMPLATE

Page 17: Angular 2 MVD workshop

GUIDELINE

● Templates files (.html)

● Global stylesheets

Page 18: Angular 2 MVD workshop

BASE TEMPLATE

1. Copy https://s3-sa-east-1.amazonaws.com/ng-music/templates/music.html

to src/client/app/music.html

2. Add reference to https://ng-music.s3-sa-east-1.amazonaws.com/app.css

in src/client/index.html

<link href="https://ng-music.s3-sa-east-1.amazonaws.com/app.css" rel="stylesheet">

Page 19: Angular 2 MVD workshop

GUIDELINE

● Templates files (.html)

● Global stylesheets

Page 20: Angular 2 MVD workshop

COMPONENTS ANDBINDING

Page 21: Angular 2 MVD workshop

COMPONENTS AND BINDING

Page 22: Angular 2 MVD workshop

GUIDELINE

Components

● Generate components

● Include components on other components

Binding

● one-way bindings using [...] notation

● define events using (...) notation

Page 23: Angular 2 MVD workshop

COMPONENTS AND BINDING

<albums-page> component

1. $ ng generate component albums-page

2. Cut/paste albums-page HTML into music/src/client/app/albums-page/albums-page.html

3. Import AlbumsPage from music/src/client/app/music.ts

4. Register AlbumsPage component as a directive

5. Use <albums-page> component in music/src/client/app/music.html template

Page 24: Angular 2 MVD workshop

COMPONENTS AND BINDING

<album-cover> component

1. $ ng generate component album-cover

2. Cut/paste album-cover HTML into music/src/client/app/album-cover/album-cover.html

3. Import AlbumCover from music/src/client/app/albums-page/albums-page.ts

4. Register AlbumCover component as a directive

5. Use <album-cover> component in music/src/client/app/albums-page/albums-page.html

Page 25: Angular 2 MVD workshop

COMPONENTS AND BINDING

Binding

1. Create dummy album on music/src/client/app/album-cover/album-cover.ts

2. Use [alt]= and [src]= notation to bind attributes

3. Use {{album.artist}} to render text on screen

4. Use (click)= notation to listen to events

Page 26: Angular 2 MVD workshop

GUIDELINE

Components

● Generate components

● Include components on other components

Binding

● one-way bindings using [...] notation

● define events using (...) notation

Page 27: Angular 2 MVD workshop

CONSUMING DATA

Page 28: Angular 2 MVD workshop

CONSUMING DATA

Page 29: Angular 2 MVD workshop

● HTTP Service

● Fetch resources from external source

● Iterate over resources using *ngFor directive

● Know about input and output properties

GUIDELINE

Page 30: Angular 2 MVD workshop

CONSUMING DATA

Fetch all albums

1. Import and register HTTP_PROVIDERS and Http class on AlbumsPage

component

2. Fetch all albums on AlbumsPage constructor

3. Restart server to proxy HTTP requests

4. $ ng server --proxy http://em-ng-workshop.herokuapp.com/

Page 31: Angular 2 MVD workshop

CONSUMING DATA

Render all albums

1. Import and register NgFor directive on AlbumsPage component

import {NgFor} from 'angular2/common';

2. Iterate over all albums on AlbumsPage template

<album-cover *ngFor="#album of albums" [album]="album"></album-cover>

Page 32: Angular 2 MVD workshop

CONSUMING DATA

Use “album” attribute

1. Import Input decorator on AlbumCover component

import {Input,Component} from 'angular2/core';

2. Decorate album property

@Input() album: any;

3. Remove dummy album definition

Page 33: Angular 2 MVD workshop

● HTTP Service

● Fetch resources from external source

● Iterate over resources using *ngFor directive

● Know about input and output properties

GUIDELINE

Page 34: Angular 2 MVD workshop

ROUTING

Page 35: Angular 2 MVD workshop

ROUTING

Page 36: Angular 2 MVD workshop

● @RouteConfig

● <router-outlet>

● [router-link]

GUIDELINE

Page 37: Angular 2 MVD workshop

ROUTING

<tracks-page> component

1. $ ng generate component tracks-page

2. copy https://s3-sa-east-1.amazonaws.com/ng-music/templates/album.html to

music/src/client/app/tracks-page/tracks-page.html

3. Import TracksPage from music/src/client/app/music.ts

4. Remove <albums-page /> component from music/src/client/app/music.html

template

Page 38: Angular 2 MVD workshop

ROUTING

@RouteConfig

1. Register in music/src/app/music.ts components in @RouteConfig

{ path: '/', component: AlbumsPage, name: 'AlbumsPage' },

{ path: '/:id', component: TracksPage, name: 'TracksPage' }

Page 39: Angular 2 MVD workshop

ROUTING

Link to go to /:id

1. Import and register routerLink directive on AlbumCover

import {RouterLink} from 'angular2/router';

2. Update AlbumCover template to add a link to TracksPage

<a [routerLink]="['TracksPage', { id: album.id }]">

3. Remove unneeded (click) event handler

Page 40: Angular 2 MVD workshop

ROUTING

Link to go back to /

1. Update Music template to add a link to AlbumsPage

<a [routerLink]="['AlbumsPage']" class="...">

<span>Albums</span>

</a>

Page 41: Angular 2 MVD workshop

● @RouteConfig

● <router-outlet>

● [router-link]

GUIDELINE

Page 42: Angular 2 MVD workshop

SERVICES

Page 43: Angular 2 MVD workshop

SERVICES

Page 44: Angular 2 MVD workshop

● Application-wide state

● Data store

GUIDELINE

Page 45: Angular 2 MVD workshop

SERVICES

1. $ ng generate service albums-service

1. Move /api/albums query to Albums service

2. Import Http from albums-service/albums-service.ts

3. Also import map: import 'rxjs/add/operator/map';

4. Create a method call getAllAlbums and return the albums

Page 46: Angular 2 MVD workshop

SERVICES

1. Import AlbumsService from albums-page/albums-page.ts

2. Add AlbumsService to the providers in AlbumsPage

3. Inject AlbumsService to the constructor of AlbumsPage

4. Subscribe from AlbumsService for changes

5. Add HTTP_PROVIDERS to music.ts

Page 47: Angular 2 MVD workshop

● Application-wide state

● Data store

GUIDELINE

Page 48: Angular 2 MVD workshop

QUESTIONS?

Page 49: Angular 2 MVD workshop