AsyncPipe

非同步管道訂閱 Observable 或 Promise 並返回它發出的最新值。發出新值時,非同步管道會標記要檢查更改的元件。當元件被銷燬時,非同步管道會自動取消訂閱以避免潛在的記憶體洩漏。

@Component({
  selector: 'async-observable-pipe',
  template: '<div><code>observable|async</code>: Time: {{ time | async }}</div>'
})
export class AsyncObservablePipeComponent {
  time = new Observable<string>((observer: Subscriber<string>) => {
    setInterval(() => observer.next(new Date().toString()), 1000);
  });
}