Skip to content

Commit fbceddd

Browse files
author
Borys Niewiadomski
committed
syntax. unsubscribe on destroy
1 parent 2340e36 commit fbceddd

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/main/web/app/hello/hello.component.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,29 @@ import {HelloService} from './hello.service';
1313
})
1414
export class HelloComponent {
1515

16+
// vars
1617
private jsonResponse: string;
1718
private message: string;
19+
private subscription;
1820

21+
// constructor
1922
constructor(private helloService: HelloService) {}
2023

24+
// on-init
2125
ngOnInit() {
22-
this.helloService.getTest().subscribe(
23-
data => {this.jsonResponse = JSON.stringify(data),
24-
this.message = data.test.message},
25-
() => console.log('../test/get/json returned: \n' + this.jsonResponse)
26+
// save subscription
27+
this.subscription = this.helloService.getTest()
28+
.subscribe(
29+
(data) => {this.jsonResponse = JSON.stringify(data);
30+
this.message = data.test.message;},
31+
(err) => console.log(err),
32+
() => console.log('hello service test complete')
2633
);
2734
}
35+
36+
// on-destroy
37+
ngOnDestroy() {
38+
// unsubscribe
39+
this.subscription.unsubscribe();
40+
}
2841
}

0 commit comments

Comments
 (0)