File tree Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -39,4 +39,7 @@ typings
39
39
40
40
# Generated files
41
41
app /** /* .js
42
- app /** /* .js.map
42
+ app /** /* .js.map
43
+
44
+ # VS Code settings
45
+ .vscode
Original file line number Diff line number Diff line change 1
- import { Component , OnInit } from '@angular/core' ;
1
+ import { Component , OnDestroy } from '@angular/core' ;
2
+ import { Subscription } from 'rxjs/Subscription' ;
2
3
3
4
import { AlertService } from '../_services/index' ;
4
5
@@ -8,12 +9,17 @@ import { AlertService } from '../_services/index';
8
9
templateUrl : 'alert.component.html'
9
10
} )
10
11
11
- export class AlertComponent {
12
+ export class AlertComponent implements OnDestroy {
13
+ private subscription : Subscription ;
12
14
message : any ;
13
15
14
- constructor ( private alertService : AlertService ) { }
16
+ constructor ( private alertService : AlertService ) {
17
+ // subscribe to alert messages
18
+ this . subscription = alertService . getMessage ( ) . subscribe ( message => { this . message = message ; } ) ;
19
+ }
15
20
16
- ngOnInit ( ) {
17
- this . alertService . getMessage ( ) . subscribe ( message => { this . message = message ; } ) ;
21
+ ngOnDestroy ( ) : void {
22
+ // unsubscribe on destroy to prevent memory leaks
23
+ this . subscription . unsubscribe ( ) ;
18
24
}
19
25
}
You can’t perform that action at this time.
0 commit comments