Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created March 15, 2016 20:43
Show Gist options
  • Save johnlindquist/63e7db08840feaba0763 to your computer and use it in GitHub Desktop.
Save johnlindquist/63e7db08840feaba0763 to your computer and use it in GitHub Desktop.
angular2 http demo
System.config({
transpiler: 'typescript',
typescriptOptions: {
emitDecoratorMetadata: true
},
map: {
app: './src'
},
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
}
}
});
<!DOCTYPE html>
<html>
<head>
<title>angular2 playground</title>
<script src="https://code.angularjs.org/2.0.0-beta.9/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="config.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.9/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.9/angular2.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.9/http.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.9/router.dev.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<app></app>
</body>
</html>
import 'rxjs/Rx';
import {HTTP_PROVIDERS, Http} from 'angular2/http';
import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
@Component({
selector: 'app',
template:`<div>
<button (click)="rxjsClick$.next()">RxJS</button>
<button (click)="angularClick$.next()">Angular</button>
Custom search: <input #i type="text" (keyup.enter)="custom$.next(i.value)">
<button (click)="custom$.next(i.value)">Custom</button>
<ul *ngFor="#item of (results | async)">
{{item.title}}
</ul>
</div>`
})
export class App{
url$ = Observable.of('http://api.stackexchange.com/2.2/search?site=stackoverflow');
rxjsClick$ = new Subject().map(()=> 'rxjs');
angularClick$ = new Subject().map(()=> 'angular');
custom$ = new Subject();
results;
constructor(http:Http){
this.results = Observable.merge(
this.rxjsClick$,
this.angularClick$,
this.custom$
)
.combineLatest(this.url$, (intitle, url)=> url + `&intitle=${intitle}`)
.switchMap(url => http.get(url))
.map(res => res.json())
.map(body => body.items)
}
}
bootstrap(App, [HTTP_PROVIDERS]);
import 'rxjs/Rx';
import {HTTP_PROVIDERS, Http} from 'angular2/http';
import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
@Component({
selector: 'app',
template:`<div>
<button (click)="rxjsClick$.next()">RxJS</button>
<button (click)="angularClick$.next()">Angular</button>
Custom search: <input #i type="text" (keyup.enter)="custom$.next(i.value)">
<button (click)="custom$.next(i.value)">Custom</button>
<ul *ngFor="#item of (results | async)">
{{item.title}}
</ul>
</div>`
})
export class App{
url$ = Observable.of('http://api.stackexchange.com/2.2/search?site=stackoverflow');
rxjsClick$ = new Subject().map(()=> 'rxjs');
angularClick$ = new Subject().map(()=> 'angular');
custom$ = new Subject();
results;
constructor(http:Http){
this.results = Observable.merge(
this.rxjsClick$,
this.angularClick$,
this.custom$
)
.combineLatest(this.url$, (intitle, url)=> url + `&intitle=${intitle}`)
.switchMap(url => http.get(url))
.map(res => res.json())
.map(body => body.items)
}
}
bootstrap(App, [HTTP_PROVIDERS]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment