Skip to content

Commit 62462c8

Browse files
committed
Updated alert component
1 parent 563cd68 commit 62462c8

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ typings
3939

4040
# Generated files
4141
app/**/*.js
42-
app/**/*.js.map
42+
app/**/*.js.map
43+
44+
# VS Code settings
45+
.vscode

app/_directives/alert.component.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnDestroy } from '@angular/core';
2+
import { Subscription } from 'rxjs/Subscription';
23

34
import { AlertService } from '../_services/index';
45

@@ -8,12 +9,17 @@ import { AlertService } from '../_services/index';
89
templateUrl: 'alert.component.html'
910
})
1011

11-
export class AlertComponent {
12+
export class AlertComponent implements OnDestroy {
13+
private subscription: Subscription;
1214
message: any;
1315

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+
}
1520

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();
1824
}
1925
}

0 commit comments

Comments
 (0)