Angular 2 Development
for Java developers
Yakov Fain, Farata Systems
@yfain
Angular 2
• Complete re-write of AngularJS
• Component-based
• Better performance: http://www.roblog.io/js-repaint-perfs
• UI rendering is implemented in a separate layer
• Reactive extensions RxJS
• Can write apps in TypeScript, 

Dart, or JavaScript (ES5 or ES6)
https://github.com/microsoft/typescript/issues/6508
Getting to know TypeScript
http://www.typescriptlang.org
What’s TypeScript?
• Designed by Anders Hejlsberg, the creator of C#, Delphi,
and Turbo Pascal
• A superset of JavaScript
• A lot more toolable than JavaScript
• Easy to learn for Java developers
• Supports types, classes, interfaces, generics, annotations
• Great IDE support
Transpiling TypeScript Interactively

http://www.typescriptlang.org/play/
TypeScript JavaScript (ES5)
Compile-time 

error
Run-time 

error
Classes
TypeScript JavaScript (ES5)
A Class With Constructor
TypeScript JavaScript (ES5)
Inheritance
Classical syntax Prototypal
Generics
Error
No Errors
Arrow Function Expressions (lambdas)
var getName = () => 'John Smith';
console.log(`The name is ` + getName());
Interfaces as Custom Types
Interfaces and implements
Back to Angular 2
What’s a Component?
• A class annotated with @Component
• Can have inputs and outputs
• Has lifecycle hooks
@Component({

selector: 'hello-world',

template: '<h1>Hello {{ name }}!</h1>'

})

class HelloWorldComponent {

name: string;



constructor() {

this.name = 'Angular 2';

}

}
In HTML:



<hello-world></hello-world>
An app is a tree of components
HTML
// import …



@Component({

selector: 'auction-application',

templateUrl: 'app/components/application/application.html',

directives: [

NavbarComponent,

FooterComponent,

SearchComponent,

HomeComponent

]

})

@Routes([

{path: '/', component: HomeComponent},

{path: '/products/:prodTitle', component: ProductDetailComponent}

])

export default class ApplicationComponent {}
HTML
Router
Configuration
Component Router: main elements
• @Routes - map URLs to components to render inside the <router-outlet>

• RouterLink ([routerLink]) - a link to a named route



• RouterOutlet (<router-outlet>) - where the router should render the
component
• RouteSegment - a map of key-value pairs to pass parameters to the route
template: `<a [routerLink]="['/Home']">Home</a>

<a [routerLink]="['/ProductDetail', {id:1234}]">Product Details</a>`
@Routes([

{path: '/', component: HomeComponent},

{path: '/product/:id', component: ProductDetailComponentParam}

]
constructor(params: RouteSegment){

this.productID = params.getParam('id');

}
template: `…
<router-outlet></router-outlet>

`
import …



@Component({

selector: 'auction-home-page',

directives: [

CarouselComponent,

ProductItemComponent

],

styleUrls: ['app/components/home/home.css'],

template: `

<div class="row carousel-holder">

<div>

<auction-carousel></auction-carousel>

</div>

</div>

<div>

<div *ngFor=“let product of products">

<auction-product-item [product]="product">

</auction-product-item>

</div>

</div>

`

})

export default class HomeComponent {

products: Product[] = [];



constructor(private productService: ProductService) {

this.products = productService.getProducts();

}

}
HomeComponent
DI
Unidirectional Binding
From code to template:
<h1>Hello {{ name }}!</h1>
<span [hidden]=“isZipcodeValid”>Zip code is not valid</span>
From template to code:
<button (click)="placeBid()">Place Bid</button>
<input placeholder= "Product name" (input)="onInputEvent()">
Properties
Events
Two-way Binding
Synchronizing Model and View:
<input [(ngModel)] = "myComponentProperty">
The value property of the <input> and
myComponentProperty will be always in sync
Dependency Injection
• Angular injects objects into components only via
constructors
• Each component has its own injector
• An injector looks at the provider to see how to inject
• Specify a provider either on the component or in one of its
anscestors
ProductService




export class ProductService {

getProduct(): Product {

return new Product( 0, "iPhone 7", 249.99,
"The latest iPhone, 7-inch screen");

}

}
export class Product {



constructor(

public id: number,

public title: string,

public price: number,

public description: string) {

}

}
Injecting ProductService
import {Component, bind} from ‘@angular/core';

import {ProductService, Product} from "../services/product-service";



@Component({

selector: 'di-product-page',

template: `<div>

<h1>Product Details</h1>

<h2>Title: {{product.title}}</h2>

<h2>Description: {{product.description}}</h2>



</div>`,

providers:[ProductService]

})



export default class ProductComponent {

product: Product;



constructor( productService: ProductService) {



this.product = productService.getProduct();

}

}

A provider can be a
class, factory, or a value
An injection point
Injecting Dependencies of Dependencies
@Injectable

export class ProductService {



constructor(private http:Http){

let products = http.get('products.json');

}

…

}
Input and Output Properties
• A component is a black box with outlets (properties)
• Properties marked as @Input() are for geting the data
into a component from the parent
• Properties marked as @Output() are for emitting events
from a component
Binding to child’s inputs
@Component({

selector: 'app',

template: `

<input type="text" placeholder="Enter stock (e.g. AAPL)" 

(change)="onInputEvent($event)">

<br/>




<order-processor [stockSymbol]=“stock"></order-processor>

`,

directives: [OrderComponent]

})

class AppComponent {

stock:string;



onInputEvent({target}):void{

this.stock=target.value;

}

}
Binding to the child’s prop
Using Destructuring
@Component({

selector: 'app',

template: `

<input type="text" placeholder="Enter stock (e.g. AAPL)" 

(change)="onInputEvent($event)">

<br/>




<order-processor [stockSymbol]=“stock"></order-processor>

`,

directives: [OrderComponent]

})

class AppComponent {

stock:string;



onInputEvent({target}):void{

this.stock=target.value;

}

}
Destructuring
Child receieves data via input properties
import {bootstrap} from ‘@angular/platform-browser-dynamic’;

import {Component, Input} from ‘@angular/core';



@Component({

selector: 'order-processor',

template: `

Buying {{quantity}} shares of {{stockSymbol}}

`})

class OrderComponent {



@Input() quantity: number;



private _stockSymbol: string;



@Input()

set stockSymbol(value: string) {

this._stockSymbol = value;



console.log(`Sending a Buy order to NASDAQ: ${this.stockSymbol} ${this.quantity}`);

}



get stockSymbol(): string {

return this._stockSymbol;

}

}
Child sends events via an output property
@Component({

selector: 'price-quoter',

template: `<strong>Inside PriceQuoterComponent:

{{stockSymbol}} {{price | currency:'USD':true:'1.2-2'}}</strong>`,



styles:[`:host {background: pink;}`]

})

class PriceQuoterComponent {



@Output() lastPrice: EventEmitter <IPriceQuote> = new EventEmitter();



stockSymbol: string = "IBM";

price:number;



constructor() {

setInterval(() => {

let priceQuote: IPriceQuote = {

stockSymbol: this.stockSymbol,



lastPrice: 100*Math.random()

};



this.price = priceQuote.lastPrice;



this.lastPrice.emit(priceQuote);



}, 1000);

}

}
A child emits events 

via output properties
Parent listens to an event
@Component({

selector: 'app',

template: `

<price-quoter (lastPrice)=“priceQuoteHandler($event)"></price-quoter><br>


AppComponent received: {{stockSymbol}} {{price | currency:’USD’:true:'1.2-2'}}
`,


directives: [PriceQuoterComponent]

})

class AppComponent {



stockSymbol: string;

price:number;



priceQuoteHandler(event:IPriceQuote) {

this.stockSymbol = event.stockSymbol;

this.price = event.lastPrice;

}

}
interface IPriceQuote {

stockSymbol: string;

lastPrice: number;

}
pipe
Projection
• A component can have only one template
• But a parent can pass HTML to the child’s template during the runtime
• Add the <ng-content> to the child’s template
template: `

<div class="wrapper">

<h2>Child</h2>

<div>This is child's content</div>



<ng-content></ng-content>



</div>`
Child
template: `

<div class="wrapper">

<h2>Parent</h2>

<div>This div is defined in the Parent's template</div>

<child>

<div>Parent projects this div onto the child </div>

</child>

</div>`
Parent
insertion point
Sample Project Structure
Type definitions
App dependencies
App code
TypeScript
config
SystemJS config
Sample Project Structure
SystemJS
transpiles TS
and loads JS
modules
bootstrap(ApplicationComponent)
Sample Project Structure
bootstrap(ApplicationComponent)
Demo
CH5: auction
npm start
https://github.com/Farata/angular2typescript
The tools that can be used today
Angular CLI is 

not ready yet

for prod
TypeScript Compiler: tsc
• Install the typescript compiler with npm (comes with Node.js):



npm install -g typescript
• Compile main.ts into main.js specifying target language syntax:



tsc —t ES5 main.ts
• Usually the compiler options are set in tsconfig.json file
• The watch mode allows to auto-compile as file changes:



tsc -w *.ts
Starting a new project with npm
1. Generate package.json for your project:

npm init -y
2. Add Angular dependencies to package.json
3. Download dependencies into the dir node_modules: 

npm install
4. Install live-server

npm install live-server -g
import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';
@Component({
selector: 'hello-world',
template: '<h1>Hello {{ name }}!</h1>'
})
class HelloWorldComponent {
name: string;
constructor() {
this.name = ‘World!';
}
}
bootstrap(HelloWorldComponent);
HelloWorldComponent in Angular: main.ts
In HTML:



<hello-world></hello-world>
package.json
{

"name": "test_samples",

"description": "A sample Weather app",

"private": true,

"scripts": {

"start": "live-server",

"test": "karma start karma.conf.js"

},

"dependencies": {

"@angular/common": "2.0.0-rc.1",

"@angular/compiler": "2.0.0-rc.1",

"@angular/core": "2.0.0-rc.1",

"@angular/http": "2.0.0-rc.1",

"@angular/platform-browser": "2.0.0-rc.1",

"@angular/platform-browser-dynamic": "2.0.0-rc.1",

"@angular/router": "2.0.0-rc.1",

"reflect-metadata": "^0.1.3",

"rxjs": "5.0.0-beta.6",

"systemjs": "^0.19.27",

"zone.js": "^0.6.12"

},

"devDependencies": {

"jasmine-core": "^2.4.1",

"karma": "^0.13.22",

"karma-chrome-launcher": "^0.2.3",

"karma-firefox-launcher": "^0.1.7",

"karma-jasmine": "^0.3.8",

"live-server": "0.8.2",

"typescript": "^1.8.10"

}

}
npm docs:
https://docs.npmjs.com
App

dependencies
Dev

dependencies
To run from

cmd window
SystemJS: a Universal Module Loader
• ES6 defines modules, but not the loader
• ES7 will include the System object for loading modules
• SystemJS is a polyfill that loads modules
<!DOCTYPE html>

<html>

<head>

<title>Basic Routing Samples</title>

<script src="node_modules/zone.js/dist/zone.js"></script>

<script src="node_modules/typescript/lib/typescript.js"></script>

<script src="node_modules/reflect-metadata/Reflect.js"></script>

<script src=“node_modules/systemjs/dist/system.src.js"></script>


<script src=“systemjs.config.js"></script>


<script>

System.import(‘app')
.catch(function (err) {console.error(err);});

</script>

</head>

<body>

<my-app></my-app>

</body>

</html>
index.html
System.config({

transpiler: 'typescript',

typescriptOptions: {emitDecoratorMetadata: true},

map: {

'app' : 'app',

'rxjs': 'node_modules/rxjs',

'@angular' : ‘node_modules/@angular’,
packages: {

'app' : {main: 'main.ts', defaultExtension: 'ts'},

'rxjs' : {main: 'index.js'},

'@angular/core' : {main: 'index.js'},

'@angular/common' : {main: 'index.js'},

'@angular/compiler' : {main: 'index.js'},

'@angular/router' : {main: 'index.js'},

'@angular/platform-browser' : {main: 'index.js'},

'@angular/platform-browser-dynamic': {main: 'index.js'},

'@angular/http' : {main: 'index.js'}



}

});
systemjs.config.js
Demo
CH9: test_weather
npm start
Reactive Extensions
• Angular comes with RxJS library of reactive extensions

• A observable function emits the data over time to a
subscriber.
• Supports multiple operators to transform the stream’s data

• Stream samples: 

- UI events

- HTTP responses

- Data arriving over websockets
Observables
• Stream elements
• Throw an error
• Send a signal that the streaming is over
An Observable object (a.k.a. producer) can:
The streaming starts when
your code invokes subscribe()
on the observable
Observers
• A function to handle the next object from the stream
• A function for error handling
• A function for end-of-stream handling
An Observer object (a.k.a. consumer) provides:
An Operator
Observable Observable
A transforming

function
transforms an observable into another observable
A map() operator
http://rxmarbles.com
A filter() operator
Operator chaining: map and filter
Observables in the UI@Component({

selector: "app",

template: `

<h2>Observable events demo</h2>

<input type="text" placeholder="Enter stock" [ngFormControl]="searchInput">

`

})

class AppComponent {



searchInput: Control;



constructor(){

this.searchInput = new Control('');



this.searchInput.valueChanges

.debounceTime(500)

.subscribe(stock => this.getStockQuoteFromServer(stock));

}



getStockQuoteFromServer(stock) {



console.log(`The price of ${stock} is ${100*Math.random().toFixed(4)}`);

}

}
Observable
Http and Observables


class AppComponent {



products: Array<string> = [];



constructor(private http: Http) {



this.http.get(‘http://localhost:8080/products')

.map(res => res.json())

.subscribe(

data => {



this.products=data;

},



err =>

console.log("Can't get products. Error code: %s, URL: %s ",

err.status, err.url),



() => console.log('Product(s) are retrieved')

);

}

}
DI
O
b
s
e
r
v
e
r
Change Detection
• Zone.js monitors all async events
• Every component has its own change detector
• CD is unidirectional and makes a single pass
• The changes can come only from a component
• Change detection runs from top to bottom of the component tree
• OnPush - don’t run change detection unless bindings to input props change
changeDetection: ChangeDetectionStrategy.OnPush
Component Lifecycle
Building and Deploying Apps
with Webpack
Objectives
• A SystemJS-based project makes too many requests and downloads
megabytes



We want:
• Minimize the number of requests
• Reduce the download size
• Automate the build in dev and prod
Webpack bundler
1. Gained popularity after it was adopted by Instagram
2. It’s a powerful and production-ready tool
3. The config file is compact
4. Has its own loader (doesn’t use SystemJS)
Webpack Hello World
<!DOCTYPE html>

<html>

<head></head>

<body>

<script src="/dist/bundle.js"></script>

</body>

</html>
module.exports = {

entry: './main',

output: {

path: './dist',

filename: 'bundle.js'

},

watch: true,

devServer: {

contentBase: '.'

}

};
document.write('Hello World!');
main.js
index.htmlwebpack.config.js
webpack main.js bundle.js
Create a bundle:
vendor.bundle.js.gz
bundle.js.gz
frameworks
app code
index.html
Source Code Deployed Code
Webpack Dev Server
const path = require('path');



const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');

const CompressionPlugin = require('compression-webpack-plugin');

const CopyWebpackPlugin = require('copy-webpack-plugin');

const DedupePlugin = require('webpack/lib/optimize/DedupePlugin');

const DefinePlugin = require('webpack/lib/DefinePlugin');

const OccurenceOrderPlugin = require('webpack/lib/optimize/OccurenceOrderPlugin');

const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');



const ENV = process.env.NODE_ENV = 'production';

const metadata = {

env: ENV

};



module.exports = {

debug: false,

devtool: 'source-map',

entry: {

'main' : './src/main.ts',

'vendor': './src/vendor.ts'

},

metadata: metadata,

module: {

loaders: [

{test: /.css$/, loader: 'to-string!css', exclude: /node_modules/},

{test: /.css$/, loader: 'style!css', exclude: /src/},

{test: /.html$/, loader: 'raw'},

{test: /.ts$/, loader: 'ts', query: {compilerOptions: {noEmit: false}}}

]

},

output: {

path : './dist',

filename: 'bundle.js'

},

plugins: [

new CommonsChunkPlugin({name: 'vendor', filename: 'vendor.bundle.js', minChunks: Infinity}),

new CompressionPlugin({regExp: /.css$|.html$|.js$|.map$/}),

new CopyWebpackPlugin([{from: './src/index.html', to: 'index.html'}]),

new DedupePlugin(),

new DefinePlugin({'webpack': {'ENV': JSON.stringify(metadata.env)}}),

new OccurenceOrderPlugin(true),

new UglifyJsPlugin({

compress: {screw_ie8 : true},

mangle: {screw_ie8 : true}

})

],

resolve: {extensions: ['', '.ts', '.js']}

};
webpack.prod.config
index.html after Webpack build
<!DOCTYPE html>

<html>

<head>

<meta charset=UTF-8>

<title>Angular Webpack Starter</title>

<base href="/">

</head>

<body>

<my-app>Loading...</my-app>

<script src="vendor.bundle.js"></script>

<script src="bundle.js"></script>

</body>

</html>

Demo

Chapter 10, angular2-webpack-starter
https://github.com/Farata/angular2typescript/tree/master/chapter10/angular2-webpack-starter
Links
• Angular 2 online workshop, starts on June 12:

http://bit.ly/1pZICMP 

discount code: jeeconf
• Code samples: https://github.com/farata
• Our company: faratasystems.com
• Blog: yakovfain.com 

discount code: faindz

More Related Content

PDF
Overview of the AngularJS framework
PDF
Angular 4 for Java Developers
PDF
Web sockets in Angular
PPTX
PDF
Reactive Thinking in Java with RxJava2
PDF
RESTful services and OAUTH protocol in IoT
PPTX
Angular js 2
PDF
Tech Webinar: Angular 2, Introduction to a new framework
Overview of the AngularJS framework
Angular 4 for Java Developers
Web sockets in Angular
Reactive Thinking in Java with RxJava2
RESTful services and OAUTH protocol in IoT
Angular js 2
Tech Webinar: Angular 2, Introduction to a new framework

What's hot (20)

PDF
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
PDF
Seven Versions of One Web Application
PDF
Intro to JavaScript
PPTX
Single Page Applications with AngularJS 2.0
PDF
Angular 2 Essential Training
PDF
Angular 2 - The Next Framework
PDF
Angular 2: core concepts
PDF
Exploring Angular 2 - Episode 2
PPTX
Introduction to angular with a simple but complete project
PDF
Exploring Angular 2 - Episode 1
PPTX
Async patterns in javascript
PPTX
Angular 4
PPTX
AngularJs presentation
PPTX
Angular2 for Beginners
PPTX
Angular 4 Introduction Tutorial
PDF
Dart for Java Developers
ODP
Introduction to Angular 2
PDF
Integrating consumers IoT devices into Business Workflow
PDF
Introduction to Angular 2
PDF
Speed up your Web applications with HTML5 WebSockets
Understanding Angular 2 - Shmuela Jacobs - Codemotion Milan 2016
Seven Versions of One Web Application
Intro to JavaScript
Single Page Applications with AngularJS 2.0
Angular 2 Essential Training
Angular 2 - The Next Framework
Angular 2: core concepts
Exploring Angular 2 - Episode 2
Introduction to angular with a simple but complete project
Exploring Angular 2 - Episode 1
Async patterns in javascript
Angular 4
AngularJs presentation
Angular2 for Beginners
Angular 4 Introduction Tutorial
Dart for Java Developers
Introduction to Angular 2
Integrating consumers IoT devices into Business Workflow
Introduction to Angular 2
Speed up your Web applications with HTML5 WebSockets
Ad

Similar to Angular2 Development for Java developers (20)

PDF
better-apps-angular-2-day1.pdf and home
PDF
Commit University - Exploring Angular 2
PDF
Angular2 with type script
PDF
Angular 2 overview in 60 minutes
PPT
17612235.ppt
PDF
Angular 2 for Java Developers
PPTX
Angular 2 in-1
PPTX
Angular 2 KTS
PPTX
Building a TV show with Angular, Bootstrap, and Web Services
PPTX
Moving From AngularJS to Angular 2
PPTX
yrs of IT experience in enterprise programming
PDF
Angular2
PPTX
Angular2 + rxjs
PPTX
An afternoon with angular 2
PPTX
An evening with Angular 2
PDF
Angular JS2 Training Session #2
PPTX
Introduction to Angular2
PPTX
Angular crash course
DOCX
Angular Interview Questions & Answers
better-apps-angular-2-day1.pdf and home
Commit University - Exploring Angular 2
Angular2 with type script
Angular 2 overview in 60 minutes
17612235.ppt
Angular 2 for Java Developers
Angular 2 in-1
Angular 2 KTS
Building a TV show with Angular, Bootstrap, and Web Services
Moving From AngularJS to Angular 2
yrs of IT experience in enterprise programming
Angular2
Angular2 + rxjs
An afternoon with angular 2
An evening with Angular 2
Angular JS2 Training Session #2
Introduction to Angular2
Angular crash course
Angular Interview Questions & Answers
Ad

More from Yakov Fain (13)

PDF
Type script for_java_dev_jul_2020
PDF
Using JHipster for generating Angular/Spring Boot apps
PDF
Using JHipster for generating Angular/Spring Boot apps
PDF
TypeScript for Java Developers
PDF
Reactive Streams and RxJava2
PDF
Using JHipster 4 for generating Angular/Spring Boot apps
PDF
Reactive programming in Angular 2
PDF
Reactive Thinking in Java
PDF
Java Intro: Unit1. Hello World
PDF
Running a Virtual Company
PDF
Princeton jug git_github
PDF
Surviving as a Professional Software Developer
PDF
Becoming a professional software developer
Type script for_java_dev_jul_2020
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
TypeScript for Java Developers
Reactive Streams and RxJava2
Using JHipster 4 for generating Angular/Spring Boot apps
Reactive programming in Angular 2
Reactive Thinking in Java
Java Intro: Unit1. Hello World
Running a Virtual Company
Princeton jug git_github
Surviving as a Professional Software Developer
Becoming a professional software developer

Recently uploaded (20)

DOC
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
PPTX
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
PDF
E-Commerce Website Development Companyin india
PDF
Workplace Software and Skills - OpenStax
PDF
AI-Powered Fuzz Testing: The Future of QA
PPTX
Full-Stack Developer Courses That Actually Land You Jobs
PDF
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
PPTX
Cybersecurity: Protecting the Digital World
PPTX
CNN LeNet5 Architecture: Neural Networks
PPTX
Lecture 5 Software Requirement Engineering
PPTX
Matchmaking for JVMs: How to Pick the Perfect GC Partner
PDF
Visual explanation of Dijkstra's Algorithm using Python
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
Airline CRS | Airline CRS Systems | CRS System
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PPTX
Introduction to Windows Operating System
PPTX
Trending Python Topics for Data Visualization in 2025
PPTX
GSA Content Generator Crack (2025 Latest)
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
E-Commerce Website Development Companyin india
Workplace Software and Skills - OpenStax
AI-Powered Fuzz Testing: The Future of QA
Full-Stack Developer Courses That Actually Land You Jobs
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
Cybersecurity: Protecting the Digital World
CNN LeNet5 Architecture: Neural Networks
Lecture 5 Software Requirement Engineering
Matchmaking for JVMs: How to Pick the Perfect GC Partner
Visual explanation of Dijkstra's Algorithm using Python
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Airline CRS | Airline CRS Systems | CRS System
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Introduction to Windows Operating System
Trending Python Topics for Data Visualization in 2025
GSA Content Generator Crack (2025 Latest)
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access

Angular2 Development for Java developers

  • 1. Angular 2 Development for Java developers Yakov Fain, Farata Systems @yfain
  • 2. Angular 2 • Complete re-write of AngularJS • Component-based • Better performance: http://www.roblog.io/js-repaint-perfs • UI rendering is implemented in a separate layer • Reactive extensions RxJS • Can write apps in TypeScript, 
 Dart, or JavaScript (ES5 or ES6) https://github.com/microsoft/typescript/issues/6508
  • 3. Getting to know TypeScript http://www.typescriptlang.org
  • 4. What’s TypeScript? • Designed by Anders Hejlsberg, the creator of C#, Delphi, and Turbo Pascal • A superset of JavaScript • A lot more toolable than JavaScript • Easy to learn for Java developers • Supports types, classes, interfaces, generics, annotations • Great IDE support
  • 7. A Class With Constructor TypeScript JavaScript (ES5)
  • 10. Arrow Function Expressions (lambdas) var getName = () => 'John Smith'; console.log(`The name is ` + getName());
  • 14. What’s a Component? • A class annotated with @Component • Can have inputs and outputs • Has lifecycle hooks @Component({
 selector: 'hello-world',
 template: '<h1>Hello {{ name }}!</h1>'
 })
 class HelloWorldComponent {
 name: string;
 
 constructor() {
 this.name = 'Angular 2';
 }
 } In HTML:
 
 <hello-world></hello-world>
  • 15. An app is a tree of components
  • 16. HTML
  • 17. // import …
 
 @Component({
 selector: 'auction-application',
 templateUrl: 'app/components/application/application.html',
 directives: [
 NavbarComponent,
 FooterComponent,
 SearchComponent,
 HomeComponent
 ]
 })
 @Routes([
 {path: '/', component: HomeComponent},
 {path: '/products/:prodTitle', component: ProductDetailComponent}
 ])
 export default class ApplicationComponent {} HTML Router Configuration
  • 18. Component Router: main elements • @Routes - map URLs to components to render inside the <router-outlet>
 • RouterLink ([routerLink]) - a link to a named route
 
 • RouterOutlet (<router-outlet>) - where the router should render the component • RouteSegment - a map of key-value pairs to pass parameters to the route template: `<a [routerLink]="['/Home']">Home</a>
 <a [routerLink]="['/ProductDetail', {id:1234}]">Product Details</a>` @Routes([
 {path: '/', component: HomeComponent},
 {path: '/product/:id', component: ProductDetailComponentParam}
 ] constructor(params: RouteSegment){
 this.productID = params.getParam('id');
 } template: `… <router-outlet></router-outlet>
 `
  • 19. import …
 
 @Component({
 selector: 'auction-home-page',
 directives: [
 CarouselComponent,
 ProductItemComponent
 ],
 styleUrls: ['app/components/home/home.css'],
 template: `
 <div class="row carousel-holder">
 <div>
 <auction-carousel></auction-carousel>
 </div>
 </div>
 <div>
 <div *ngFor=“let product of products">
 <auction-product-item [product]="product">
 </auction-product-item>
 </div>
 </div>
 `
 })
 export default class HomeComponent {
 products: Product[] = [];
 
 constructor(private productService: ProductService) {
 this.products = productService.getProducts();
 }
 } HomeComponent DI
  • 20. Unidirectional Binding From code to template: <h1>Hello {{ name }}!</h1> <span [hidden]=“isZipcodeValid”>Zip code is not valid</span> From template to code: <button (click)="placeBid()">Place Bid</button> <input placeholder= "Product name" (input)="onInputEvent()"> Properties Events
  • 21. Two-way Binding Synchronizing Model and View: <input [(ngModel)] = "myComponentProperty"> The value property of the <input> and myComponentProperty will be always in sync
  • 22. Dependency Injection • Angular injects objects into components only via constructors • Each component has its own injector • An injector looks at the provider to see how to inject • Specify a provider either on the component or in one of its anscestors
  • 23. ProductService 
 
 export class ProductService {
 getProduct(): Product {
 return new Product( 0, "iPhone 7", 249.99, "The latest iPhone, 7-inch screen");
 }
 } export class Product {
 
 constructor(
 public id: number,
 public title: string,
 public price: number,
 public description: string) {
 }
 }
  • 24. Injecting ProductService import {Component, bind} from ‘@angular/core';
 import {ProductService, Product} from "../services/product-service";
 
 @Component({
 selector: 'di-product-page',
 template: `<div>
 <h1>Product Details</h1>
 <h2>Title: {{product.title}}</h2>
 <h2>Description: {{product.description}}</h2>
 
 </div>`,
 providers:[ProductService]
 })
 
 export default class ProductComponent {
 product: Product;
 
 constructor( productService: ProductService) {
 
 this.product = productService.getProduct();
 }
 }
 A provider can be a class, factory, or a value An injection point
  • 25. Injecting Dependencies of Dependencies @Injectable
 export class ProductService {
 
 constructor(private http:Http){
 let products = http.get('products.json');
 }
 …
 }
  • 26. Input and Output Properties • A component is a black box with outlets (properties) • Properties marked as @Input() are for geting the data into a component from the parent • Properties marked as @Output() are for emitting events from a component
  • 27. Binding to child’s inputs @Component({
 selector: 'app',
 template: `
 <input type="text" placeholder="Enter stock (e.g. AAPL)" 
 (change)="onInputEvent($event)">
 <br/> 
 
 <order-processor [stockSymbol]=“stock"></order-processor>
 `,
 directives: [OrderComponent]
 })
 class AppComponent {
 stock:string;
 
 onInputEvent({target}):void{
 this.stock=target.value;
 }
 } Binding to the child’s prop
  • 28. Using Destructuring @Component({
 selector: 'app',
 template: `
 <input type="text" placeholder="Enter stock (e.g. AAPL)" 
 (change)="onInputEvent($event)">
 <br/> 
 
 <order-processor [stockSymbol]=“stock"></order-processor>
 `,
 directives: [OrderComponent]
 })
 class AppComponent {
 stock:string;
 
 onInputEvent({target}):void{
 this.stock=target.value;
 }
 } Destructuring
  • 29. Child receieves data via input properties import {bootstrap} from ‘@angular/platform-browser-dynamic’;
 import {Component, Input} from ‘@angular/core';
 
 @Component({
 selector: 'order-processor',
 template: `
 Buying {{quantity}} shares of {{stockSymbol}}
 `})
 class OrderComponent {
 
 @Input() quantity: number;
 
 private _stockSymbol: string;
 
 @Input()
 set stockSymbol(value: string) {
 this._stockSymbol = value;
 
 console.log(`Sending a Buy order to NASDAQ: ${this.stockSymbol} ${this.quantity}`);
 }
 
 get stockSymbol(): string {
 return this._stockSymbol;
 }
 }
  • 30. Child sends events via an output property @Component({
 selector: 'price-quoter',
 template: `<strong>Inside PriceQuoterComponent:
 {{stockSymbol}} {{price | currency:'USD':true:'1.2-2'}}</strong>`,
 
 styles:[`:host {background: pink;}`]
 })
 class PriceQuoterComponent {
 
 @Output() lastPrice: EventEmitter <IPriceQuote> = new EventEmitter();
 
 stockSymbol: string = "IBM";
 price:number;
 
 constructor() {
 setInterval(() => {
 let priceQuote: IPriceQuote = {
 stockSymbol: this.stockSymbol,
 
 lastPrice: 100*Math.random()
 };
 
 this.price = priceQuote.lastPrice;
 
 this.lastPrice.emit(priceQuote);
 
 }, 1000);
 }
 } A child emits events 
 via output properties
  • 31. Parent listens to an event @Component({
 selector: 'app',
 template: `
 <price-quoter (lastPrice)=“priceQuoteHandler($event)"></price-quoter><br> 
 AppComponent received: {{stockSymbol}} {{price | currency:’USD’:true:'1.2-2'}} `, 
 directives: [PriceQuoterComponent]
 })
 class AppComponent {
 
 stockSymbol: string;
 price:number;
 
 priceQuoteHandler(event:IPriceQuote) {
 this.stockSymbol = event.stockSymbol;
 this.price = event.lastPrice;
 }
 } interface IPriceQuote {
 stockSymbol: string;
 lastPrice: number;
 } pipe
  • 32. Projection • A component can have only one template • But a parent can pass HTML to the child’s template during the runtime • Add the <ng-content> to the child’s template template: `
 <div class="wrapper">
 <h2>Child</h2>
 <div>This is child's content</div>
 
 <ng-content></ng-content>
 
 </div>` Child template: `
 <div class="wrapper">
 <h2>Parent</h2>
 <div>This div is defined in the Parent's template</div>
 <child>
 <div>Parent projects this div onto the child </div>
 </child>
 </div>` Parent insertion point
  • 33. Sample Project Structure Type definitions App dependencies App code TypeScript config SystemJS config
  • 34. Sample Project Structure SystemJS transpiles TS and loads JS modules bootstrap(ApplicationComponent)
  • 37. The tools that can be used today Angular CLI is 
 not ready yet
 for prod
  • 38. TypeScript Compiler: tsc • Install the typescript compiler with npm (comes with Node.js):
 
 npm install -g typescript • Compile main.ts into main.js specifying target language syntax:
 
 tsc —t ES5 main.ts • Usually the compiler options are set in tsconfig.json file • The watch mode allows to auto-compile as file changes:
 
 tsc -w *.ts
  • 39. Starting a new project with npm 1. Generate package.json for your project:
 npm init -y 2. Add Angular dependencies to package.json 3. Download dependencies into the dir node_modules: 
 npm install 4. Install live-server
 npm install live-server -g
  • 40. import {bootstrap} from 'angular2/platform/browser'; import {Component} from 'angular2/core'; @Component({ selector: 'hello-world', template: '<h1>Hello {{ name }}!</h1>' }) class HelloWorldComponent { name: string; constructor() { this.name = ‘World!'; } } bootstrap(HelloWorldComponent); HelloWorldComponent in Angular: main.ts In HTML:
 
 <hello-world></hello-world>
  • 41. package.json {
 "name": "test_samples",
 "description": "A sample Weather app",
 "private": true,
 "scripts": {
 "start": "live-server",
 "test": "karma start karma.conf.js"
 },
 "dependencies": {
 "@angular/common": "2.0.0-rc.1",
 "@angular/compiler": "2.0.0-rc.1",
 "@angular/core": "2.0.0-rc.1",
 "@angular/http": "2.0.0-rc.1",
 "@angular/platform-browser": "2.0.0-rc.1",
 "@angular/platform-browser-dynamic": "2.0.0-rc.1",
 "@angular/router": "2.0.0-rc.1",
 "reflect-metadata": "^0.1.3",
 "rxjs": "5.0.0-beta.6",
 "systemjs": "^0.19.27",
 "zone.js": "^0.6.12"
 },
 "devDependencies": {
 "jasmine-core": "^2.4.1",
 "karma": "^0.13.22",
 "karma-chrome-launcher": "^0.2.3",
 "karma-firefox-launcher": "^0.1.7",
 "karma-jasmine": "^0.3.8",
 "live-server": "0.8.2",
 "typescript": "^1.8.10"
 }
 } npm docs: https://docs.npmjs.com App
 dependencies Dev
 dependencies To run from
 cmd window
  • 42. SystemJS: a Universal Module Loader • ES6 defines modules, but not the loader • ES7 will include the System object for loading modules • SystemJS is a polyfill that loads modules
  • 43. <!DOCTYPE html>
 <html>
 <head>
 <title>Basic Routing Samples</title>
 <script src="node_modules/zone.js/dist/zone.js"></script>
 <script src="node_modules/typescript/lib/typescript.js"></script>
 <script src="node_modules/reflect-metadata/Reflect.js"></script>
 <script src=“node_modules/systemjs/dist/system.src.js"></script> 
 <script src=“systemjs.config.js"></script> 
 <script>
 System.import(‘app') .catch(function (err) {console.error(err);});
 </script>
 </head>
 <body>
 <my-app></my-app>
 </body>
 </html> index.html
  • 44. System.config({
 transpiler: 'typescript',
 typescriptOptions: {emitDecoratorMetadata: true},
 map: {
 'app' : 'app',
 'rxjs': 'node_modules/rxjs',
 '@angular' : ‘node_modules/@angular’, packages: {
 'app' : {main: 'main.ts', defaultExtension: 'ts'},
 'rxjs' : {main: 'index.js'},
 '@angular/core' : {main: 'index.js'},
 '@angular/common' : {main: 'index.js'},
 '@angular/compiler' : {main: 'index.js'},
 '@angular/router' : {main: 'index.js'},
 '@angular/platform-browser' : {main: 'index.js'},
 '@angular/platform-browser-dynamic': {main: 'index.js'},
 '@angular/http' : {main: 'index.js'}
 
 }
 }); systemjs.config.js
  • 46. Reactive Extensions • Angular comes with RxJS library of reactive extensions
 • A observable function emits the data over time to a subscriber. • Supports multiple operators to transform the stream’s data
 • Stream samples: 
 - UI events
 - HTTP responses
 - Data arriving over websockets
  • 47. Observables • Stream elements • Throw an error • Send a signal that the streaming is over An Observable object (a.k.a. producer) can: The streaming starts when your code invokes subscribe() on the observable
  • 48. Observers • A function to handle the next object from the stream • A function for error handling • A function for end-of-stream handling An Observer object (a.k.a. consumer) provides:
  • 49. An Operator Observable Observable A transforming
 function transforms an observable into another observable
  • 52. Operator chaining: map and filter
  • 53. Observables in the UI@Component({
 selector: "app",
 template: `
 <h2>Observable events demo</h2>
 <input type="text" placeholder="Enter stock" [ngFormControl]="searchInput">
 `
 })
 class AppComponent {
 
 searchInput: Control;
 
 constructor(){
 this.searchInput = new Control('');
 
 this.searchInput.valueChanges
 .debounceTime(500)
 .subscribe(stock => this.getStockQuoteFromServer(stock));
 }
 
 getStockQuoteFromServer(stock) {
 
 console.log(`The price of ${stock} is ${100*Math.random().toFixed(4)}`);
 }
 } Observable
  • 54. Http and Observables 
 class AppComponent {
 
 products: Array<string> = [];
 
 constructor(private http: Http) {
 
 this.http.get(‘http://localhost:8080/products')
 .map(res => res.json())
 .subscribe(
 data => {
 
 this.products=data;
 },
 
 err =>
 console.log("Can't get products. Error code: %s, URL: %s ",
 err.status, err.url),
 
 () => console.log('Product(s) are retrieved')
 );
 }
 } DI O b s e r v e r
  • 55. Change Detection • Zone.js monitors all async events • Every component has its own change detector • CD is unidirectional and makes a single pass • The changes can come only from a component • Change detection runs from top to bottom of the component tree • OnPush - don’t run change detection unless bindings to input props change changeDetection: ChangeDetectionStrategy.OnPush
  • 57. Building and Deploying Apps with Webpack
  • 58. Objectives • A SystemJS-based project makes too many requests and downloads megabytes
 
 We want: • Minimize the number of requests • Reduce the download size • Automate the build in dev and prod
  • 59. Webpack bundler 1. Gained popularity after it was adopted by Instagram 2. It’s a powerful and production-ready tool 3. The config file is compact 4. Has its own loader (doesn’t use SystemJS)
  • 60. Webpack Hello World <!DOCTYPE html>
 <html>
 <head></head>
 <body>
 <script src="/dist/bundle.js"></script>
 </body>
 </html> module.exports = {
 entry: './main',
 output: {
 path: './dist',
 filename: 'bundle.js'
 },
 watch: true,
 devServer: {
 contentBase: '.'
 }
 }; document.write('Hello World!'); main.js index.htmlwebpack.config.js webpack main.js bundle.js Create a bundle:
  • 63. const path = require('path');
 
 const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
 const CompressionPlugin = require('compression-webpack-plugin');
 const CopyWebpackPlugin = require('copy-webpack-plugin');
 const DedupePlugin = require('webpack/lib/optimize/DedupePlugin');
 const DefinePlugin = require('webpack/lib/DefinePlugin');
 const OccurenceOrderPlugin = require('webpack/lib/optimize/OccurenceOrderPlugin');
 const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
 
 const ENV = process.env.NODE_ENV = 'production';
 const metadata = {
 env: ENV
 };
 
 module.exports = {
 debug: false,
 devtool: 'source-map',
 entry: {
 'main' : './src/main.ts',
 'vendor': './src/vendor.ts'
 },
 metadata: metadata,
 module: {
 loaders: [
 {test: /.css$/, loader: 'to-string!css', exclude: /node_modules/},
 {test: /.css$/, loader: 'style!css', exclude: /src/},
 {test: /.html$/, loader: 'raw'},
 {test: /.ts$/, loader: 'ts', query: {compilerOptions: {noEmit: false}}}
 ]
 },
 output: {
 path : './dist',
 filename: 'bundle.js'
 },
 plugins: [
 new CommonsChunkPlugin({name: 'vendor', filename: 'vendor.bundle.js', minChunks: Infinity}),
 new CompressionPlugin({regExp: /.css$|.html$|.js$|.map$/}),
 new CopyWebpackPlugin([{from: './src/index.html', to: 'index.html'}]),
 new DedupePlugin(),
 new DefinePlugin({'webpack': {'ENV': JSON.stringify(metadata.env)}}),
 new OccurenceOrderPlugin(true),
 new UglifyJsPlugin({
 compress: {screw_ie8 : true},
 mangle: {screw_ie8 : true}
 })
 ],
 resolve: {extensions: ['', '.ts', '.js']}
 }; webpack.prod.config
  • 64. index.html after Webpack build <!DOCTYPE html>
 <html>
 <head>
 <meta charset=UTF-8>
 <title>Angular Webpack Starter</title>
 <base href="/">
 </head>
 <body>
 <my-app>Loading...</my-app>
 <script src="vendor.bundle.js"></script>
 <script src="bundle.js"></script>
 </body>
 </html>

  • 66. Links • Angular 2 online workshop, starts on June 12:
 http://bit.ly/1pZICMP 
 discount code: jeeconf • Code samples: https://github.com/farata • Our company: faratasystems.com • Blog: yakovfain.com 
 discount code: faindz