@Component({ selector: 'file-detail', template: ` <span>Last Modified:</span> <p>{{file?.lastModified | date}}</p> ` }) class FileDetailComponent { }
@Component({ selector: 'file-detail', template: ` <span>Last Modified:</span> <p>{{file?.lastModified | date:"MM/dd/yy"}}</p> ` }) class FileDetailComponent { }
@Component({ selector: 'file-detail', template: ` <span>Last Modified:</span> <p>{{file?.lastModified | date:"MM/dd/yy"}}</p> <input [(ngModel)]="format"> ` }) class FileDetailComponent { }
@Component({ ... template: ` <span>Last Modified:</span> <p>{{ expression | replace:pattern:replacement}}</p> ` }) class FileDetailComponent { }
@Component({ template: ` <span>Made with:</span> <p>Hello {{name | replace:pattern:'2.0'}}</p>`}) class ReplacePipeComponent { constructor() { this.name = 'Angular 1.5' this.pattern = new RegExp(/(?:\d*\.)?\d+/g); } }
@Component({ selector: 'file-detail', template: ` <span>File:</span> <p>{{file | json}}</p> ` }) class FileDetailComponent { }
import {Pipe,PipeTransform} from 'angular2/core' @Pipe({name: 'trim'}) export class TrimPipe implements PipeTransform { transform(str:string, [length]) : string { if (!str || !length || str.length <= length) { return (str || ''); } let dots = length <= 3 ? '' : '...'; return str.substr(0, length) + dots; } }
import {Pipe,PipeTransform} from 'angular2/core' @Pipe({name: 'trim'}) export class TrimPipe implements PipeTransform { transform(str:string, [length]) : string { if (!str || !length || str.length <= length) { return (str || ''); } let dots = length <= 3 ? '' : '...'; return str.substr(0, length) + dots; } }
import {Pipe,PipeTransform} from 'angular2/core' @Pipe({name: 'trim'}) export class TrimPipe implements PipeTransform { transform(str:string, [length]) : string { if (!str || !length || str.length <= length) { return (str || ''); } let dots = length <= 3 ? '' : '...'; return str.substr(0, length) + dots; } }
import {Pipe, PipeTransform} from 'angular2/core'; @Pipe({name: 'shared'}) export class SharedPipe implements PipeTransform { transform(files:File[]) { return files.filter(file => file.shared); } }
<input type=checbox> [(ngModel)]="file.shared" {{file.name}}
Angular executes a pure pipe only when it detects a pure change to the input value.
class FilesComponent { constructor(){...} this.files.push(newFile); }
@Pipe({name: 'shared', pure: false}) export class SharedPipe implements PipeTransform { transform(files:File[]) { return files.filter(file => file.shared); } }
import {Observable} from 'rxjs/Observable';
@Component(...)
class FilesListComponent {
contacts: Observable<Array<File>>;
constructor(filesService: FilesService) {
this.files = filesService.getFiles();
}
}
import {Observable} from 'rxjs/Observable'; @Component(...) class FilesListComponent { contacts: Observable<Array<Contact>>; constructor(filesService: FilesService) { this.files = filesService.getFiles(); } }
@Component({
template:
<ul>
<li *ngFor="let file of files | async">
...
</li>
</ul>
`
})
class FilesListComponent {...}
@Component({ template: ` <ul> <li *ngFor="let file of files | async"> ... </li> </ul> ` }) class FilesListComponent {...}
@Pipe({name:
'fetch'})
export class FetchJsonPipe implements PipeTransform{
private fetched:any = null;
constructor(private _http: Http) { }
transform(url:string):any {
this.fetched = null;
this._http.get(url)
.map( result => result.json() )
.subscribe( result => this.fetched = result )
}
return this.fetched;
}
}
{{file.name}}
{{file.name}}