Skip to content

Commit 645b2cd

Browse files
committed
Update text
1 parent 404c986 commit 645b2cd

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,23 @@ When you should unsubscribe when the component gets destroyed.
6666
# Recommended ways to unsubscribe
6767
One way is to assign the subscription to a class property and manually unsubscribe in ``ngOnDestroy``.
6868
```
69+
private subscription: Subscription;
70+
71+
ngOnInit() {
72+
this.subscription = timer(0, 1000)
73+
.subscribe(() => {
74+
this.counter++;
75+
this.titleService.setTitle('Counter ' + this.counter);
76+
});
77+
}
78+
6979
ngOnDestroy() {
7080
// Avoid side effects and memory leak by unsubscribing:
7181
this.subscription.unsubscribe();
7282
}
7383
```
7484

75-
However, its not feasible for large applications with many subscriptions,
85+
However, it's not feasible for large applications with many subscriptions,
7686
as its cumbersome to write and introduces a lot of obfuscating code.
7787

7888
Another way of handling it, would be to collect all subscriptions and unsubscribe them at once:
@@ -133,7 +143,7 @@ ngOnDestroy() {
133143
}
134144
```
135145
When the component gets destroyed, the observable ``ngDestroy`` gets completed, causing the subscriptions to complete.
136-
Thus memory leaks and side effects are avoided.
146+
Thus memory leaks and unwanted side effects are avoided.
137147

138148
Drawbacks: As with the other methods, it's still quite verbose and error-prone.
139149

0 commit comments

Comments
 (0)