Skip to main content

Posts

Showing posts with the label What is async pipe?

What is Angular 2 Async Pipe?

What is Async Pipe? Angular 2 provides us special kinds of pipe that is called Async pipe and the Async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. The Async pipe allows us to bind our templates directly to values that arrive asynchronously manner and this is the great ability for the promises and observables. Stayed Informed – What is Pipes? Example for AsyncPipe with Promise using NgFor, @Component({     selector: 'app-promise' ,     template: '<ul> < li * ngFor="let user of users | async">  Id: {{user.id }}, Name: {{user.name }} </li>< /ul>' }) export class PromiseComponent {     //USERS DECLARATIONS.     users = [];     //FETCHING JSON DATA FROM REST APIS     userRestApiUrl: string = 'https://api.github.com/users/hadley/orgs' ;     //HOME COMPONENT CONSTRUCTO...

Angular 2 Async Pipe with Ngfor Observable

What is async pipe? The async pipe subscribes to an Observable and returns the latest changed value. When a new value is changed, the async pipe marks the component to be checked for changes. When the component gets destroyed the async pipe unsubscribes automatically to avoid potential memory leaks. How to use async pipe in Angular 2? observable_expression | async Example, @ 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 ); }); } The async pipe with ngFor in Angular 2, * ngFor = "#obj of (myAsyncObject | async)?.prop1?.prop2" Reference, https://angular.io/docs/ts/latest/api/common/index/AsyncPipe-...