angular-cn/public/docs/_examples/pipes/ts/app/fetch-json.pipe.ts
Patrice Chalin ad95b04e69 docs(dev guide): pipes - new Dart prose, update Dart and Ts code (#1353)
+ guide/pipes/ts: update docs and example code

+ guide/pipes/dart: new prose, updated example code

+ fix platform_directives reference; html cleanup

+ enable pipes e2e testing
For `e2e-spec.js`: If the async test is executed too early it will fail
(simply because the async message hasn’t been received yet).

+ follow new constants naming convention
2016-05-13 21:44:14 +01:00

29 lines
664 B
TypeScript

// #docregion
import { Pipe, PipeTransform } from '@angular/core';
import { Http } from '@angular/http';
// #docregion pipe-metadata
@Pipe({
name: 'fetch',
pure: false
})
// #enddocregion pipe-metadata
export class FetchJsonPipe implements PipeTransform{
private fetchedJson: any = null;
private prevUrl = '';
constructor(private _http: Http) { }
transform(url: string): any {
if (url !== this.prevUrl) {
this.prevUrl = url;
this.fetchedJson = null;
this._http.get(url)
.map( result => result.json() )
.subscribe( result => this.fetchedJson = result );
}
return this.fetchedJson;
}
}