The AsyncPipe is used to subscribe to an observable or promise and return the latest value it has emitted. When a new value is emitted, the pipe marks the component that has been checked for changes.
See the following example where a time observable continuously updates the view for every 2 seconds with the current time.
Example:
- @Component({
- selector: ‘async-observable-pipe’,
- template: `<div><code>observable|async</code>:
- Time: {{ time | async }}</div>`
- })
- export class AsyncObservablePipeComponent {
- time = new Observable(observer =>
- setInterval(() => observer.next(new Date().toString()), 2000)
- );
- }