<!DOCTYPE html>
<html>
<head>
<base href="." />
<title>angular2 playground</title>
<script data-require="tether@*" data-semver="1.3.4" src="//cdnjs.cloudflare.com/ajax/libs/tether/1.3.1/js/tether.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/zone.js/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>
loading...
</my-app>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
</body>
</html>
/* Styles go here */
body {
font-family: 'Ubuntu', Arial, Helvetica, sans-serif; }
### Angular Starter Plunker - Typescript
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
//map tells the System loader where to look for things
map: {
'app': './src',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.0.2/lib/typescript.js'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {AppModule} from './app';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { DemoComponent } from './demo.component'
@Component({
selector: 'my-app',
template: `
<div>
<h2>{{name}}</h2>
</div>
<br><br>
<demo></demo>
<br><br>
`,
})
export class App {
name:string;
constructor() {
this.name = 'Angular2 Demos'
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App,
DemoComponent],
bootstrap: [ App ]
})
export class AppModule {}
import { Component, OnInit, Input, ViewChild, ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/observable/merge';
import 'rxjs/add/operator/scan';
import 'rxjs/add/operator/map';
@Component({
selector: 'demo',
template: `
<div>{{demoInfo}}</div>
<button #incrementButton id="incrementButton" class="btn btn-outline-success" value="1">Increment</button>
<span #totalSpan id="totalSpan" style="margin-left:10px;">0</span>
<button #decrementButton id="decrementButton" class="btn btn-outline-success" style="margin-left:10px;" value="-1">Decrement</button>
<button id="addListenerButton" class="btn btn-secondary" (click)="addListeners()" [disabled]=listenersActive>Add Listeners</button>
<button id="removeListenerButton" class="btn btn-secondary" (click)="removeListeners()" [disabled]=!listenersActive>Remove Listeners</button>
<button class="btn btn-primary" (click)="reset()">Reset</button>
<div *ngFor="let output of outputArray">
{{output}}
</div>
`,
})
export class DemoComponent implements OnInit{
demoInfo = 'Observable Merge & Scan (Reduce)';
outputArray = new Array();
@ViewChild('incrementButton') incrementButton: ElementRef;
@ViewChild('decrementButton') decrementButton: ElementRef;
@ViewChild('totalSpan') totalSpan: ElementRef;
listenersActive: boolean = true;
getValue = ev => parseInt(ev.target.value, 10);
increment$: Observable<{}>;
decrement$: Observable<{}>;
changes$: Observable<{}>;
total$: Observable<{}>;
changeListener;
totalListener;
ngOnInit(){
//establish streams of click events
this.increment$ = Observable.fromEvent(this.incrementButton.nativeElement, 'click');
this.decrement$ = Observable.fromEvent(this.decrementButton.nativeElement, 'click');
//merge them into a single stream of changes
this.changes$ = Observable.merge(this.increment$, this.decrement$).map(this.getValue);
//reduce the stream of changes to a running total
this.total$ = this.changes$.scan((total, value) => (<number>total) + (<number>value), 0);
this.addListeners();
}
addListeners(){
//remove any previously subscribed listeners
this.removeListeners();
this.outputArray.push("Adding Change Listener");
this.changeListener = this.changes$.subscribe(val => console.log(val));
this.outputArray.push("Adding Total Listener");
this.totalListener = this.total$.subscribe(count => (<HTMLSpanElement> this.totalSpan.nativeElement).innerText = count.toString());
this.listenersActive = true;
}
removeListeners(){
if (this.changeListener != null){
this.outputArray.push("Unsubscribing Change Listener");
this.changeListener.unsubscribe();
}
if (this.totalListener != null){
this.outputArray.push("Unsubscribing Total Listener");
this.totalListener.unsubscribe();
}
(<HTMLSpanElement> this.totalSpan.nativeElement).innerText = "0";
this.listenersActive = false;
}
reset(){
this.addListeners();
this.outputArray.length = 0;
}
}