Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit c9d529f

Browse files
Added more prose for managing subscriptions and async pipe
1 parent 798f48a commit c9d529f

File tree

4 files changed

+205
-108
lines changed

4 files changed

+205
-108
lines changed

Diff for: public/docs/_examples/rxjs/ts/app/app.module.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { LoadingComponent } from './loading.component';
1111
import { HeroSearchComponent } from './hero-search.component';
1212
import { HeroDetailComponent } from './hero-detail.component';
1313
import { HeroListComponent } from './hero-list.component';
14-
14+
import { HeroCounterComponent } from './hero-counter.component';
1515
import { LoadingService } from './loading.service';
1616
import { HeroService } from './hero.service';
1717

@@ -34,7 +34,8 @@ import { ApiErrorHandlerService } from './api-error-handler.service';
3434
LoadingComponent,
3535
HeroSearchComponent,
3636
HeroDetailComponent,
37-
HeroListComponent
37+
HeroListComponent,
38+
HeroCounterComponent
3839
],
3940
providers: [
4041
HeroService,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// // #docplaster
2+
// // #docregion
3+
// import { Component, OnInit } from '@angular/core';
4+
//
5+
// import { HeroService } from './hero.service';
6+
// import { Hero } from './hero';
7+
//
8+
// @Component({
9+
// template: `
10+
// <h2>HEROES</h2>
11+
// <ul class="items">
12+
// <li *ngFor="let hero of heroes">
13+
// <span class="badge">{{ hero.id }}</span> {{ hero.name }}
14+
// </li>
15+
// </ul>
16+
// `
17+
// })
18+
// export class HeroListComponent implements OnInit {
19+
// heroes: Hero[];
20+
//
21+
// constructor(
22+
// private service: HeroService
23+
// ) {}
24+
//
25+
// ngOnInit() {
26+
// this.service.getHeroes()
27+
// .subscribe(heroes => this.heroes = heroes);
28+
// }
29+
// }
30+
// // #enddocregion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// #docplaster
2+
// #docregion
3+
import { Component, OnInit } from '@angular/core';
4+
import { Subject } from 'rxjs/Subject';
5+
import { Observable } from 'rxjs/Observable';
6+
import { Observer } from 'rxjs/Observer';
7+
import { Subscription } from 'rxjs/Subscription';
8+
9+
import { HeroService } from './hero.service';
10+
import { Hero } from './hero';
11+
12+
@Component({
13+
selector: 'hero-counter',
14+
template: `
15+
<h2>HERO COUNTER</h2>
16+
<p>
17+
Heroes {{ count }}
18+
</p>
19+
`
20+
})
21+
export class HeroCounterComponent implements OnInit {
22+
count: number = 0;
23+
counter$: Observable<number>;
24+
sub: Subscription;
25+
destroy$ = new Subject();
26+
27+
ngOnInit() {
28+
this.counter$ = Observable.create((observer: Observer<number>) => {
29+
const interval = setInterval(() => {
30+
observer.next(this.count++);
31+
}, 1000);
32+
});
33+
34+
this.counter$
35+
.takeUntil(this.destroy$)
36+
.subscribe();
37+
}
38+
39+
ngOnDestroy() {
40+
this.destroy$.next();
41+
}
42+
}
43+
// #enddocregion

0 commit comments

Comments
 (0)