SlideShare a Scribd company logo
1 of 67
Angular 4
Quick introduction for AngularJS 1.5 developers
Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
Angular 4
Quick introduction for AngularJS 1.5 developers
a.k.a.
How to not write a large or scalable web applications.
Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
Angular 4
Quick introduction for AngularJS 1.5 developers
a.k.a.
How to not write a large or scalable web applications.
Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
Angular 4
Quick introduction for AngularJS 1.5 developers
a.k.a.
How to not write a large or scalable web applications.
Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
Agenda
• @AboutMe()
• Javascript ecosystem (prepare the environment during this part)
• Trends
• Modules
• Javascript fatigue
• Workshop
• Boilerplate
• Blueprints
• Typescript/Angular
• Building blocks – service, component, dependency injection
• Q&A
Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
@AboutMe()
• I started programming 18 years ago
• I code for money from the 9 years
• I code for Decerto 3 years
• I was programming in Pascal, C, PHP, Javascript, Bash, SQL, C++, Java, Typescript
• I did some AJAX before it was popular
• I did some UX before it was so popular
• I did some Devops before it was so named
• I was programming SPAs over the last 4 years in AngularJS
• I observe the development of Angular 2 since AngularJS 1.5-beta.0
• I am doing SPA in Angular 2 the last few months
• I switch to Angular 4 in the coming weeks
Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
@AboutMe()
• I started programming 18 years ago
• I code for money from the 9 years
• I code for Decerto 3 years
• I was programming in Pascal, C, PHP, Javascript, Bash, SQL, C++, Java, Typescript
• I did some AJAX before it was popular
• I did some UX before it was so popular
• I did some Devops before it was so named
• I was programming SPAs over the last 4 years in AngularJS
• I observe the development of Angular 2 since AngularJS 1.5-beta.0
• I am doing SPA in Angular 2 the last few months
• I switch to Angular 4 in the coming weeks
Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
Workshop (It is the time to prepare environment)
• You should have installed:
• node >= 6.9.0 (https://nodejs.org/  download and install LTS)
• npm >= 3.0.0 (bundled with node)
• git
• bash (winbash, gitbash, zsh)
• typescript aware IDE
• Intellij Idea Ultimate / Intellij Idea Ultimate EAP
• Microsoft Visual Studio Code
• Microsoft Visual Studio
• Atom
• Your favorite
Task 0 – Hello world
Task 0 – Hello world
• npm install --global @angular/cli@latest # first time only
• ng new --ng4 my-project # first time only
# Let scripts do the work…
Trends
Trends trends
Trends trends trends
Trends trends trends trends
Trends trends trends trends trends
Trends trends trends trends trends trends
Trends…
• Trends proves (almost) every thesis
• angular and react in game
• It’s hard to say which is better
• Angularjs migrates to Angular
• Jquery still popular but it is going down
Task 0 – Hello world (remind)
• npm install --global @angular/cli@latest # first time only
• ng new --ng4 my-project # first time only
# Let scripts do the work…
There are frameworks
• Angular
• AngularJS
• Aurelia
• Backbone
• CycleJs
• Elm
• EmberJs
• ExtJs
• Knockout
• Meteor
• Mithril
• Polymer
• Preact
• React
• VueJs
There are packages managers
• Bower
• Npm
• (WebJars)
• Yarn
(modules without Javascript ecosystem)
(modules without Javascript ecosystem)
Task 0 – Hello world (remind)
• npm install --global @angular/cli@latest # first time only
• ng new --ng4 my-project # first time only
# Let scripts do the work…
There are languages
• ClojureScript
• CofeeScript
• Dart
• Ecmascript 5
• Ecmascript 2015
• Ecmasrcipt 2016
• Elm
• Typescript
There are compilers/transpilers
• Babel
• Clojure
• CoffeScript
• Dart
• Elm
• Emscripten
• Google Closure Compiler
• Typescript
There are static analyzis tools
• Codelyzer
• JSHint
• JSLint
• TSLint
There are template engines
• HandleBars
• Jade
• JSX
There are unit test frameworks
• Ava
• Chai
• Jasmine
• Mocha
• Qunit
• Sinon.js
• Wallaby*
There are styling languages/tools
• CSS
• compass
• Less
• PostCSS
• Sass
• Scss
• Stylus
There are boilerplate tools
• @angular/cli
• Seed projects
• yeoman
There are task runners
• (Gradle)
• Grunt
• Gulp
• (Maven)
• (Makefile)
• Npm
There are bundlers
• Brocholi
• Brunch
• Browserify
• Google Closure
• JSPM
• Rollup
• Webpack
• SystemJS
There are core JS Libraries
• Bacon
• Bluebird
• D3
• Hammerjs
• Jquery
• Lodash
• Lunr.js
• Modernizr
• Moment
• Rambda
• RxJs
• Three.js
• Underscore
• Zepto
There is Javascript Fatigue
https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f
Angular
• @angular/cli*
• CSS or SCSS – the only one true decision to make
• Codelyzer
• Jasmine
• Npm
• RxJs
• Webpack
• TSLint
• Typescript*
* You can replace, but you propably don’t want to
Workshop/Demo
• You should have installed:
• node >= 6.9.0 (https://nodejs.org/  download and install LTS)
• npm >= 3.0.0 (budled with node)
• git
• bash (winbash, gitbash, zsh)
• typescript aware IDE
• Intellij Idea Ultimate / Intellij Idea Ultimate EAP
• Microsoft Visual Studio Code
• Microsoft Visual Studio
• Atom
• Your favorite
Task 0 – Hello world (remind)
• npm install --global @angular/cli@latest # first time only
• ng new --ng4 my-project # first time only
Boilerplate, scripts, blueprints
• npm run build
• npm run test
• npm run lint
• npm run e2e
• npm run ng make-this-awesome
• npm run start -- --port 4444
• npm run ng -- generate <class | component | directive | enum |
guard | interface | module | pipe | service> [options] name
Typescript (open app.component.ts)
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'app works!';
}
ES2015 import modules Exported symbol from module Module in node_modules
Decorator
(not an annotation!)
POJO
ES2015 export modules
Syntactic sugar for
Object with prototypal inharitance
String property on instance
Task 1 – Count to a random number
Task 1 – Count to a random number
AppComponent
CounterComponent
RandomNumberGeneratorService
Plain old button
Task 1 – Count to a random number
• Create counter component
• Create Random Number Generator Interface
• Define the inferface
• Create a RNG Service
• Implement counter
• Generate random number
• Handle click
Task 1 – create counter component
• npm run ng -- generate component --inline-style --inline-template
counter
Typescript (open counter.component.ts)
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<p>
counter Works!
</p>
`,
styles: []
})
export class CounterComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
Template string
Constructor
Method Interface
Task 1 – create interface
• npm run ng -- generate interface random-number-generator
Typescript (replace random-number-generator.ts)
export interface RandomNumberGenerator {
readonly name: string;
readonly infoUrl: string;
next (state: any): {state: any, value: number};
reset (seed?: any): any;
}
Readonly property… …of type string
Return type
OptionalMethod signature Value of any type
(Simpliest ever random number generator)
(Simpliest ever random number generator)
https://xkcd.com/221/
Task 1 – create service
• npm run ng -- generate service xkcd-rng
import {Injectable} from '@angular/core';
import {RandomNumberGenerator} from './random-number-generator';
@Injectable()
export class XkcdRngService implements RandomNumberGenerator {
readonly infoUrl = 'https://xkcd.com/221/';
readonly name = 'XKCD Random Number Generator';
reset() {
return null;
}
next(state: any = null) {
return {state, value: 4};
}
}
Typescript (replace xkcd-rng.service.ts)
Default value
Property shorthand
Task 1 – implement counter
Angular (replace counter.component.ts)
import {Component, Input, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'app-counter',
template: `{{ tick | async }}`,
})
export class CounterComponent {
@Input() counter = 5;
@Input() delay = 90;
@Output() finish = new EventEmitter<boolean>();
@Output() tick = new EventEmitter<number>();
public start(): void {
this.count(0);
}
private count(n: number): void {
this.tick.emit(n);
if (n >= this.counter) {
this.finish.emit(true);
} else {
setTimeout(() => this.count(n + 1), this.delay);
}
}
}
How to not write a large or scalable web applications.
It will count to 5
unless you specify
otherwise
It will be placed in <app-counter> element
It will display current tick number whenever it comes
It’s a component
It will wait 90ms
between ticks
unless you specify
otherwise
It will tell you about finish
It will tell you about tick
You can start counting down
Look! There is no $apply!
Task 1 – generate random number
Angular (replace app.component.ts, add XkcdRngService in providers in AppModule)
import {Component, OnInit} from '@angular/core';
import {XkcdRngService} from './xkcd-rng.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
counter: number;
constructor(public xkcd: XkcdRngService) {
}
ngOnInit(): void {
this.counter = this.xkcd.next().value;
}
}
Dependency injection
Assigned to component
if public/private specified
Lifecycle hook
providers: [
XkcdRngService,
],
Task 1 – handle click
Angular (replace app.component.{html,css}
<section>
<button (click)="cnt.start()">Start</button>
<h2>
<app-counter #cnt
[counter]="counter"></app-counter>
</h2>
</section>
h2 {
display: inline-block;
margin: 0;
}
button {
margin: 10px;
padding: 10px;
}
Input in app-counter Property from app-root
Handle output
Local template variable
- definition and usage
Access target component API
Task 2 – Multiple count to a random numbers
Task 2 – Multiple count to a random numbers
Task 1
RNG name and link
Random
independent
counters
Configurable
Task 2 – Multiple count to a random numbers
• Create another RNG service (this time configurable)
• Multiple counters
• Inject service
• Make service injection configurable which depends on environment
Task 2 – Create another RNG service
• npm run ng -- generate service lcg-rng-factory
Typescript / Angular (replace lcg-rng-factory.service.ts)
import {Injectable} from '@angular/core';
import {RandomNumberGenerator} from './random-number-generator';
interface LcgState { prev: number; multiplier: number; increment: number; modulo: number; }
const LCG_INFO_URL = 'https://en.wikipedia.org/wiki/Linear_congruential_generator';
@Injectable()
export class LcgRngFactoryService {
private createLcg(name: string, initialState: LcgState): RandomNumberGenerator {
return { infoUrl: LCG_INFO_URL, name,
reset: (seed = 1) => Object.assign({}, initialState, {prev: seed}),
next: (state: LcgState = initialState) => {
const prev = (state.multiplier * state.prev + state.increment) % state.modulo;
return {value: prev, state: Object.assign({}, state, {prev})};
}
};
}
public glibc() { return this.createLcg('glibc', {prev: 1, multiplier: 1103515245, increment: 12345, modulo: 2 ** 31 - 1}); }
public random0(){ return this.createLcg('random0', {prev: 1, multiplier: 8121, increment: 28411, modulo: 134456}); }
public minstd(){ return this.createLcg('minstd_rand', {prev: 1, multiplier: 48271, increment: 0, modulo: 2 ** 31 - 1}); }
}
Local
interface
Local
constant
Block
scope
variable Fat arrow function ("fart") – function without this
Angular (replace app.component.html)
<h1>RNG: <a [href]="rng.infoUrl">{{ rng.name }}</a></h1>
<section *ngFor="let counter of numbers">
<button (click)="cnt.start()">Start</button>
<h2>
<app-counter #cnt
[counter]="counter"></app-counter>
</h2>
</section>
ng-repeat replacement
Local variable
No changes here
Task 2 – Injectable RNG service
Angular – DI (replace app.component.ts body)
public numbers: number[];
private rngState: any;
constructor(public xkcd: XkcdRngService, @Inject('RNG') public rng: RandomNumberGenerator) { }
ngOnInit(): void {
this.rngState = this.rng.reset();
const length = 1 + this.xkcd.next().value % 10;
this.numbers = Array.from({length}).map(() => this.random());
}
private random(n = 50): number {
const {state, value} = this.rng.next(this.rngState);
this.rngState = state;
return value % n;
}
DI symbol
How to not write a large or scalable web applications.
export function rngFactory(lcg: LcgRngFactoryService, x: XkcdRngService){
if(environment.production){
return lcg.glibc();
}else{
return x;
}
}
@NgModule({
// ...
providers: [
XkcdRngService,
LcgRngFactoryService,
// {provide: 'RNG', useClass: XkcdRngService},
{provide: 'RNG', useFactory: rngFactory, deps: [LcgRngFactoryService, XkcdRngService]},
],
// ...
})
export class AppModule { }
Angular – DI (replace app.module.ts providers)
DI symbol
Comes from src/enviroments/*.ts
Serve in two variants of the enviroment
• npm run start -- --port 4444
• npm run start -- --port 4444 -e prod
Q&A
Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23

More Related Content

What's hot

Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2Knoldus Inc.
 
Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next FrameworkCommit University
 
Introduction to angular 2
Introduction to angular 2Introduction to angular 2
Introduction to angular 2Dhyego Fernando
 
Developing a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2IDeveloping a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2INader Debbabi
 
Angular 4 fronts
Angular 4 frontsAngular 4 fronts
Angular 4 frontsbadal dubla
 
Exploring Angular 2 - Episode 1
Exploring Angular 2 - Episode 1Exploring Angular 2 - Episode 1
Exploring Angular 2 - Episode 1Ahmed Moawad
 
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016Codemotion
 
An Intro to Angular 2
An Intro to Angular 2An Intro to Angular 2
An Intro to Angular 2Ron Heft
 
Angular 2 for Java Developers
Angular 2 for Java DevelopersAngular 2 for Java Developers
Angular 2 for Java DevelopersYakov Fain
 
Angular 2... so can I use it now??
Angular 2... so can I use it now??Angular 2... so can I use it now??
Angular 2... so can I use it now??Laurent Duveau
 
Angular2 with TypeScript
Angular2 with TypeScript Angular2 with TypeScript
Angular2 with TypeScript Rohit Bishnoi
 
Angular4 getting started
Angular4 getting startedAngular4 getting started
Angular4 getting startedTejinderMakkar
 
Angular 2: What's New?
Angular 2: What's New?Angular 2: What's New?
Angular 2: What's New?jbandi
 
Exploring Angular 2 - Episode 2
Exploring Angular 2 - Episode 2Exploring Angular 2 - Episode 2
Exploring Angular 2 - Episode 2Ahmed Moawad
 
Angular 4 Introduction Tutorial
Angular 4 Introduction TutorialAngular 4 Introduction Tutorial
Angular 4 Introduction TutorialScott Lee
 
Angular 6 - The Complete Guide
Angular 6 - The Complete GuideAngular 6 - The Complete Guide
Angular 6 - The Complete GuideSam Dias
 
Angular 2: core concepts
Angular 2: core conceptsAngular 2: core concepts
Angular 2: core conceptsCodemotion
 
Angular 2 - Core Concepts
Angular 2 - Core ConceptsAngular 2 - Core Concepts
Angular 2 - Core ConceptsFabio Biondi
 
Express: A Jump-Start
Express: A Jump-StartExpress: A Jump-Start
Express: A Jump-StartNaveen Pete
 

What's hot (20)

Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 
Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next Framework
 
Introduction to angular 2
Introduction to angular 2Introduction to angular 2
Introduction to angular 2
 
Developing a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2IDeveloping a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2I
 
Angular 4 fronts
Angular 4 frontsAngular 4 fronts
Angular 4 fronts
 
Exploring Angular 2 - Episode 1
Exploring Angular 2 - Episode 1Exploring Angular 2 - Episode 1
Exploring Angular 2 - Episode 1
 
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
 
An Intro to Angular 2
An Intro to Angular 2An Intro to Angular 2
An Intro to Angular 2
 
Angular 2 for Java Developers
Angular 2 for Java DevelopersAngular 2 for Java Developers
Angular 2 for Java Developers
 
Angular 4 - quick view
Angular 4 - quick viewAngular 4 - quick view
Angular 4 - quick view
 
Angular 2... so can I use it now??
Angular 2... so can I use it now??Angular 2... so can I use it now??
Angular 2... so can I use it now??
 
Angular2 with TypeScript
Angular2 with TypeScript Angular2 with TypeScript
Angular2 with TypeScript
 
Angular4 getting started
Angular4 getting startedAngular4 getting started
Angular4 getting started
 
Angular 2: What's New?
Angular 2: What's New?Angular 2: What's New?
Angular 2: What's New?
 
Exploring Angular 2 - Episode 2
Exploring Angular 2 - Episode 2Exploring Angular 2 - Episode 2
Exploring Angular 2 - Episode 2
 
Angular 4 Introduction Tutorial
Angular 4 Introduction TutorialAngular 4 Introduction Tutorial
Angular 4 Introduction Tutorial
 
Angular 6 - The Complete Guide
Angular 6 - The Complete GuideAngular 6 - The Complete Guide
Angular 6 - The Complete Guide
 
Angular 2: core concepts
Angular 2: core conceptsAngular 2: core concepts
Angular 2: core concepts
 
Angular 2 - Core Concepts
Angular 2 - Core ConceptsAngular 2 - Core Concepts
Angular 2 - Core Concepts
 
Express: A Jump-Start
Express: A Jump-StartExpress: A Jump-Start
Express: A Jump-Start
 

Viewers also liked

Architektura ngrx w angular 2+
Architektura ngrx w angular 2+Architektura ngrx w angular 2+
Architektura ngrx w angular 2+Paweł Żurowski
 
Dive into Angular, part 4: Angular 2.0
Dive into Angular, part 4: Angular 2.0Dive into Angular, part 4: Angular 2.0
Dive into Angular, part 4: Angular 2.0Oleksii Prohonnyi
 
Angular 4 for Java Developers
Angular 4 for Java DevelopersAngular 4 for Java Developers
Angular 4 for Java DevelopersYakov Fain
 
Getting Started with Angular 2
Getting Started with Angular 2Getting Started with Angular 2
Getting Started with Angular 2FITC
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painSander Mak (@Sander_Mak)
 
Building Universal Applications with Angular 2
Building Universal Applications with Angular 2Building Universal Applications with Angular 2
Building Universal Applications with Angular 2Minko Gechev
 
Hello AngularJS - Back to the future
Hello AngularJS - Back to the futureHello AngularJS - Back to the future
Hello AngularJS - Back to the futureOuadie LAHDIOUI
 
Dive into Angular, part 1: Introduction
Dive into Angular, part 1: IntroductionDive into Angular, part 1: Introduction
Dive into Angular, part 1: IntroductionOleksii Prohonnyi
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJSTu Hoang
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedVisual Engineering
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraLINAGORA
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6William Marques
 
004. Working with React component
004. Working with React component004. Working with React component
004. Working with React componentBinh Quan Duc
 
Starting with Reactjs
Starting with ReactjsStarting with Reactjs
Starting with ReactjsThinh VoXuan
 
002. Working with Webpack
002. Working with Webpack002. Working with Webpack
002. Working with WebpackBinh Quan Duc
 
001. Introduction about React
001. Introduction about React001. Introduction about React
001. Introduction about ReactBinh Quan Duc
 
Exploiting Deserialization Vulnerabilities in Java
Exploiting Deserialization Vulnerabilities in JavaExploiting Deserialization Vulnerabilities in Java
Exploiting Deserialization Vulnerabilities in JavaCODE WHITE GmbH
 

Viewers also liked (20)

Architektura ngrx w angular 2+
Architektura ngrx w angular 2+Architektura ngrx w angular 2+
Architektura ngrx w angular 2+
 
Dive into Angular, part 4: Angular 2.0
Dive into Angular, part 4: Angular 2.0Dive into Angular, part 4: Angular 2.0
Dive into Angular, part 4: Angular 2.0
 
Angular 4 for Java Developers
Angular 4 for Java DevelopersAngular 4 for Java Developers
Angular 4 for Java Developers
 
Getting Started with Angular 2
Getting Started with Angular 2Getting Started with Angular 2
Getting Started with Angular 2
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
 
Building Universal Applications with Angular 2
Building Universal Applications with Angular 2Building Universal Applications with Angular 2
Building Universal Applications with Angular 2
 
Hello AngularJS - Back to the future
Hello AngularJS - Back to the futureHello AngularJS - Back to the future
Hello AngularJS - Back to the future
 
Dive into Angular, part 1: Introduction
Dive into Angular, part 1: IntroductionDive into Angular, part 1: Introduction
Dive into Angular, part 1: Introduction
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - Linagora
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
 
panduan-google-adsense.pdf
panduan-google-adsense.pdfpanduan-google-adsense.pdf
panduan-google-adsense.pdf
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6
 
React 101
React 101React 101
React 101
 
004. Working with React component
004. Working with React component004. Working with React component
004. Working with React component
 
Starting with Reactjs
Starting with ReactjsStarting with Reactjs
Starting with Reactjs
 
002. Working with Webpack
002. Working with Webpack002. Working with Webpack
002. Working with Webpack
 
001. Introduction about React
001. Introduction about React001. Introduction about React
001. Introduction about React
 
Exploiting Deserialization Vulnerabilities in Java
Exploiting Deserialization Vulnerabilities in JavaExploiting Deserialization Vulnerabilities in Java
Exploiting Deserialization Vulnerabilities in Java
 

Similar to Quick introduction to Angular 4 for AngularJS 1.5 developers

From MEAN to the MERN Stack
From MEAN to the MERN StackFrom MEAN to the MERN Stack
From MEAN to the MERN StackTroy Miles
 
Angular meetup 2 2019-08-29
Angular meetup 2   2019-08-29Angular meetup 2   2019-08-29
Angular meetup 2 2019-08-29Nitin Bhojwani
 
Modern Web-site Development Pipeline
Modern Web-site Development PipelineModern Web-site Development Pipeline
Modern Web-site Development PipelineGlobalLogic Ukraine
 
SF Gradle Meetup - Netflix OSS
SF Gradle Meetup - Netflix OSSSF Gradle Meetup - Netflix OSS
SF Gradle Meetup - Netflix OSSJustin Ryan
 
MEAN Stack Warm-up
MEAN Stack Warm-upMEAN Stack Warm-up
MEAN Stack Warm-upTroy Miles
 
Web worker in your angular application
Web worker in your angular applicationWeb worker in your angular application
Web worker in your angular applicationSuresh Patidar
 
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработкиJS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработкиJSFestUA
 
Rapid application development with spring roo j-fall 2010 - baris dere
Rapid application development with spring roo   j-fall 2010 - baris dereRapid application development with spring roo   j-fall 2010 - baris dere
Rapid application development with spring roo j-fall 2010 - baris dereBaris Dere
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectJadson Santos
 
Getting Started with the Angular 2 CLI
Getting Started with the Angular 2 CLIGetting Started with the Angular 2 CLI
Getting Started with the Angular 2 CLIJim Lynch
 
Angular4 kickstart
Angular4 kickstartAngular4 kickstart
Angular4 kickstartFoyzul Karim
 
Angular2.0@Shanghai0319
Angular2.0@Shanghai0319Angular2.0@Shanghai0319
Angular2.0@Shanghai0319Bibby Chung
 
AzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
AzovDevMeetup 2016 | Angular 2: обзор | Александр ШевнинAzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
AzovDevMeetup 2016 | Angular 2: обзор | Александр ШевнинJSC “Arcadia Inc”
 
AngularJS One Day Workshop
AngularJS One Day WorkshopAngularJS One Day Workshop
AngularJS One Day WorkshopShyam Seshadri
 
Angular CLI : HelloWorld
Angular CLI : HelloWorldAngular CLI : HelloWorld
Angular CLI : HelloWorldnikspatel007
 
Creating Modular Test-Driven SPAs with Spring and AngularJS
Creating Modular Test-Driven SPAs with Spring and AngularJSCreating Modular Test-Driven SPAs with Spring and AngularJS
Creating Modular Test-Driven SPAs with Spring and AngularJSGunnar Hillert
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners WorkshopSathish VJ
 

Similar to Quick introduction to Angular 4 for AngularJS 1.5 developers (20)

From MEAN to the MERN Stack
From MEAN to the MERN StackFrom MEAN to the MERN Stack
From MEAN to the MERN Stack
 
Angular meetup 2 2019-08-29
Angular meetup 2   2019-08-29Angular meetup 2   2019-08-29
Angular meetup 2 2019-08-29
 
Modern Web-site Development Pipeline
Modern Web-site Development PipelineModern Web-site Development Pipeline
Modern Web-site Development Pipeline
 
React inter3
React inter3React inter3
React inter3
 
SF Gradle Meetup - Netflix OSS
SF Gradle Meetup - Netflix OSSSF Gradle Meetup - Netflix OSS
SF Gradle Meetup - Netflix OSS
 
MEAN Stack Warm-up
MEAN Stack Warm-upMEAN Stack Warm-up
MEAN Stack Warm-up
 
Web worker in your angular application
Web worker in your angular applicationWeb worker in your angular application
Web worker in your angular application
 
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработкиJS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
 
Rapid application development with spring roo j-fall 2010 - baris dere
Rapid application development with spring roo   j-fall 2010 - baris dereRapid application development with spring roo   j-fall 2010 - baris dere
Rapid application development with spring roo j-fall 2010 - baris dere
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 
Getting Started with the Angular 2 CLI
Getting Started with the Angular 2 CLIGetting Started with the Angular 2 CLI
Getting Started with the Angular 2 CLI
 
Angular4 kickstart
Angular4 kickstartAngular4 kickstart
Angular4 kickstart
 
Angular2.0@Shanghai0319
Angular2.0@Shanghai0319Angular2.0@Shanghai0319
Angular2.0@Shanghai0319
 
Angular2 inter3
Angular2 inter3Angular2 inter3
Angular2 inter3
 
AzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
AzovDevMeetup 2016 | Angular 2: обзор | Александр ШевнинAzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
AzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
 
AngularJS One Day Workshop
AngularJS One Day WorkshopAngularJS One Day Workshop
AngularJS One Day Workshop
 
Angular CLI : HelloWorld
Angular CLI : HelloWorldAngular CLI : HelloWorld
Angular CLI : HelloWorld
 
Angular js
Angular jsAngular js
Angular js
 
Creating Modular Test-Driven SPAs with Spring and AngularJS
Creating Modular Test-Driven SPAs with Spring and AngularJSCreating Modular Test-Driven SPAs with Spring and AngularJS
Creating Modular Test-Driven SPAs with Spring and AngularJS
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 

Recently uploaded

How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 

Recently uploaded (20)

How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 

Quick introduction to Angular 4 for AngularJS 1.5 developers

  • 1. Angular 4 Quick introduction for AngularJS 1.5 developers Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
  • 2. Angular 4 Quick introduction for AngularJS 1.5 developers a.k.a. How to not write a large or scalable web applications. Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
  • 3. Angular 4 Quick introduction for AngularJS 1.5 developers a.k.a. How to not write a large or scalable web applications. Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
  • 4. Angular 4 Quick introduction for AngularJS 1.5 developers a.k.a. How to not write a large or scalable web applications. Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
  • 5. Agenda • @AboutMe() • Javascript ecosystem (prepare the environment during this part) • Trends • Modules • Javascript fatigue • Workshop • Boilerplate • Blueprints • Typescript/Angular • Building blocks – service, component, dependency injection • Q&A Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
  • 6. @AboutMe() • I started programming 18 years ago • I code for money from the 9 years • I code for Decerto 3 years • I was programming in Pascal, C, PHP, Javascript, Bash, SQL, C++, Java, Typescript • I did some AJAX before it was popular • I did some UX before it was so popular • I did some Devops before it was so named • I was programming SPAs over the last 4 years in AngularJS • I observe the development of Angular 2 since AngularJS 1.5-beta.0 • I am doing SPA in Angular 2 the last few months • I switch to Angular 4 in the coming weeks Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
  • 7. @AboutMe() • I started programming 18 years ago • I code for money from the 9 years • I code for Decerto 3 years • I was programming in Pascal, C, PHP, Javascript, Bash, SQL, C++, Java, Typescript • I did some AJAX before it was popular • I did some UX before it was so popular • I did some Devops before it was so named • I was programming SPAs over the last 4 years in AngularJS • I observe the development of Angular 2 since AngularJS 1.5-beta.0 • I am doing SPA in Angular 2 the last few months • I switch to Angular 4 in the coming weeks Paweł Żurowski <zurowski.pawel@gmail.com> 2017-03-23
  • 8. Workshop (It is the time to prepare environment) • You should have installed: • node >= 6.9.0 (https://nodejs.org/  download and install LTS) • npm >= 3.0.0 (bundled with node) • git • bash (winbash, gitbash, zsh) • typescript aware IDE • Intellij Idea Ultimate / Intellij Idea Ultimate EAP • Microsoft Visual Studio Code • Microsoft Visual Studio • Atom • Your favorite
  • 9. Task 0 – Hello world
  • 10. Task 0 – Hello world • npm install --global @angular/cli@latest # first time only • ng new --ng4 my-project # first time only # Let scripts do the work…
  • 15. Trends trends trends trends trends
  • 16. Trends trends trends trends trends trends
  • 17. Trends… • Trends proves (almost) every thesis • angular and react in game • It’s hard to say which is better • Angularjs migrates to Angular • Jquery still popular but it is going down
  • 18. Task 0 – Hello world (remind) • npm install --global @angular/cli@latest # first time only • ng new --ng4 my-project # first time only # Let scripts do the work…
  • 19. There are frameworks • Angular • AngularJS • Aurelia • Backbone • CycleJs • Elm • EmberJs • ExtJs • Knockout • Meteor • Mithril • Polymer • Preact • React • VueJs
  • 20. There are packages managers • Bower • Npm • (WebJars) • Yarn
  • 23. Task 0 – Hello world (remind) • npm install --global @angular/cli@latest # first time only • ng new --ng4 my-project # first time only # Let scripts do the work…
  • 24. There are languages • ClojureScript • CofeeScript • Dart • Ecmascript 5 • Ecmascript 2015 • Ecmasrcipt 2016 • Elm • Typescript
  • 25. There are compilers/transpilers • Babel • Clojure • CoffeScript • Dart • Elm • Emscripten • Google Closure Compiler • Typescript
  • 26. There are static analyzis tools • Codelyzer • JSHint • JSLint • TSLint
  • 27. There are template engines • HandleBars • Jade • JSX
  • 28. There are unit test frameworks • Ava • Chai • Jasmine • Mocha • Qunit • Sinon.js • Wallaby*
  • 29. There are styling languages/tools • CSS • compass • Less • PostCSS • Sass • Scss • Stylus
  • 30. There are boilerplate tools • @angular/cli • Seed projects • yeoman
  • 31. There are task runners • (Gradle) • Grunt • Gulp • (Maven) • (Makefile) • Npm
  • 32. There are bundlers • Brocholi • Brunch • Browserify • Google Closure • JSPM • Rollup • Webpack • SystemJS
  • 33. There are core JS Libraries • Bacon • Bluebird • D3 • Hammerjs • Jquery • Lodash • Lunr.js • Modernizr • Moment • Rambda • RxJs • Three.js • Underscore • Zepto
  • 34. There is Javascript Fatigue https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f
  • 35. Angular • @angular/cli* • CSS or SCSS – the only one true decision to make • Codelyzer • Jasmine • Npm • RxJs • Webpack • TSLint • Typescript* * You can replace, but you propably don’t want to
  • 36. Workshop/Demo • You should have installed: • node >= 6.9.0 (https://nodejs.org/  download and install LTS) • npm >= 3.0.0 (budled with node) • git • bash (winbash, gitbash, zsh) • typescript aware IDE • Intellij Idea Ultimate / Intellij Idea Ultimate EAP • Microsoft Visual Studio Code • Microsoft Visual Studio • Atom • Your favorite
  • 37. Task 0 – Hello world (remind) • npm install --global @angular/cli@latest # first time only • ng new --ng4 my-project # first time only
  • 38. Boilerplate, scripts, blueprints • npm run build • npm run test • npm run lint • npm run e2e • npm run ng make-this-awesome • npm run start -- --port 4444 • npm run ng -- generate <class | component | directive | enum | guard | interface | module | pipe | service> [options] name
  • 39. Typescript (open app.component.ts) import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent { title = 'app works!'; } ES2015 import modules Exported symbol from module Module in node_modules Decorator (not an annotation!) POJO ES2015 export modules Syntactic sugar for Object with prototypal inharitance String property on instance
  • 40. Task 1 – Count to a random number
  • 41. Task 1 – Count to a random number AppComponent CounterComponent RandomNumberGeneratorService Plain old button
  • 42. Task 1 – Count to a random number • Create counter component • Create Random Number Generator Interface • Define the inferface • Create a RNG Service • Implement counter • Generate random number • Handle click
  • 43. Task 1 – create counter component • npm run ng -- generate component --inline-style --inline-template counter
  • 44. Typescript (open counter.component.ts) import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-counter', template: ` <p> counter Works! </p> `, styles: [] }) export class CounterComponent implements OnInit { constructor() { } ngOnInit() { } } Template string Constructor Method Interface
  • 45. Task 1 – create interface • npm run ng -- generate interface random-number-generator
  • 46. Typescript (replace random-number-generator.ts) export interface RandomNumberGenerator { readonly name: string; readonly infoUrl: string; next (state: any): {state: any, value: number}; reset (seed?: any): any; } Readonly property… …of type string Return type OptionalMethod signature Value of any type
  • 47. (Simpliest ever random number generator)
  • 48. (Simpliest ever random number generator) https://xkcd.com/221/
  • 49. Task 1 – create service • npm run ng -- generate service xkcd-rng
  • 50. import {Injectable} from '@angular/core'; import {RandomNumberGenerator} from './random-number-generator'; @Injectable() export class XkcdRngService implements RandomNumberGenerator { readonly infoUrl = 'https://xkcd.com/221/'; readonly name = 'XKCD Random Number Generator'; reset() { return null; } next(state: any = null) { return {state, value: 4}; } } Typescript (replace xkcd-rng.service.ts) Default value Property shorthand
  • 51. Task 1 – implement counter
  • 52. Angular (replace counter.component.ts) import {Component, Input, Output, EventEmitter} from '@angular/core'; @Component({ selector: 'app-counter', template: `{{ tick | async }}`, }) export class CounterComponent { @Input() counter = 5; @Input() delay = 90; @Output() finish = new EventEmitter<boolean>(); @Output() tick = new EventEmitter<number>(); public start(): void { this.count(0); } private count(n: number): void { this.tick.emit(n); if (n >= this.counter) { this.finish.emit(true); } else { setTimeout(() => this.count(n + 1), this.delay); } } } How to not write a large or scalable web applications. It will count to 5 unless you specify otherwise It will be placed in <app-counter> element It will display current tick number whenever it comes It’s a component It will wait 90ms between ticks unless you specify otherwise It will tell you about finish It will tell you about tick You can start counting down Look! There is no $apply!
  • 53. Task 1 – generate random number
  • 54. Angular (replace app.component.ts, add XkcdRngService in providers in AppModule) import {Component, OnInit} from '@angular/core'; import {XkcdRngService} from './xkcd-rng.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { counter: number; constructor(public xkcd: XkcdRngService) { } ngOnInit(): void { this.counter = this.xkcd.next().value; } } Dependency injection Assigned to component if public/private specified Lifecycle hook providers: [ XkcdRngService, ],
  • 55. Task 1 – handle click
  • 56. Angular (replace app.component.{html,css} <section> <button (click)="cnt.start()">Start</button> <h2> <app-counter #cnt [counter]="counter"></app-counter> </h2> </section> h2 { display: inline-block; margin: 0; } button { margin: 10px; padding: 10px; } Input in app-counter Property from app-root Handle output Local template variable - definition and usage Access target component API
  • 57. Task 2 – Multiple count to a random numbers
  • 58. Task 2 – Multiple count to a random numbers Task 1 RNG name and link Random independent counters Configurable
  • 59. Task 2 – Multiple count to a random numbers • Create another RNG service (this time configurable) • Multiple counters • Inject service • Make service injection configurable which depends on environment
  • 60. Task 2 – Create another RNG service • npm run ng -- generate service lcg-rng-factory
  • 61. Typescript / Angular (replace lcg-rng-factory.service.ts) import {Injectable} from '@angular/core'; import {RandomNumberGenerator} from './random-number-generator'; interface LcgState { prev: number; multiplier: number; increment: number; modulo: number; } const LCG_INFO_URL = 'https://en.wikipedia.org/wiki/Linear_congruential_generator'; @Injectable() export class LcgRngFactoryService { private createLcg(name: string, initialState: LcgState): RandomNumberGenerator { return { infoUrl: LCG_INFO_URL, name, reset: (seed = 1) => Object.assign({}, initialState, {prev: seed}), next: (state: LcgState = initialState) => { const prev = (state.multiplier * state.prev + state.increment) % state.modulo; return {value: prev, state: Object.assign({}, state, {prev})}; } }; } public glibc() { return this.createLcg('glibc', {prev: 1, multiplier: 1103515245, increment: 12345, modulo: 2 ** 31 - 1}); } public random0(){ return this.createLcg('random0', {prev: 1, multiplier: 8121, increment: 28411, modulo: 134456}); } public minstd(){ return this.createLcg('minstd_rand', {prev: 1, multiplier: 48271, increment: 0, modulo: 2 ** 31 - 1}); } } Local interface Local constant Block scope variable Fat arrow function ("fart") – function without this
  • 62. Angular (replace app.component.html) <h1>RNG: <a [href]="rng.infoUrl">{{ rng.name }}</a></h1> <section *ngFor="let counter of numbers"> <button (click)="cnt.start()">Start</button> <h2> <app-counter #cnt [counter]="counter"></app-counter> </h2> </section> ng-repeat replacement Local variable No changes here
  • 63. Task 2 – Injectable RNG service
  • 64. Angular – DI (replace app.component.ts body) public numbers: number[]; private rngState: any; constructor(public xkcd: XkcdRngService, @Inject('RNG') public rng: RandomNumberGenerator) { } ngOnInit(): void { this.rngState = this.rng.reset(); const length = 1 + this.xkcd.next().value % 10; this.numbers = Array.from({length}).map(() => this.random()); } private random(n = 50): number { const {state, value} = this.rng.next(this.rngState); this.rngState = state; return value % n; } DI symbol How to not write a large or scalable web applications.
  • 65. export function rngFactory(lcg: LcgRngFactoryService, x: XkcdRngService){ if(environment.production){ return lcg.glibc(); }else{ return x; } } @NgModule({ // ... providers: [ XkcdRngService, LcgRngFactoryService, // {provide: 'RNG', useClass: XkcdRngService}, {provide: 'RNG', useFactory: rngFactory, deps: [LcgRngFactoryService, XkcdRngService]}, ], // ... }) export class AppModule { } Angular – DI (replace app.module.ts providers) DI symbol Comes from src/enviroments/*.ts
  • 66. Serve in two variants of the enviroment • npm run start -- --port 4444 • npm run start -- --port 4444 -e prod