From 78fce3523d977e604f0c69fd98ac4ba9cde152a4 Mon Sep 17 00:00:00 2001 From: David Czeck Date: Thu, 1 Jun 2017 13:25:05 -0700 Subject: [PATCH] Track subscription for use in ngDestroy --- lib/svg-icon.component.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/svg-icon.component.ts b/lib/svg-icon.component.ts index b84a026..2f1a363 100644 --- a/lib/svg-icon.component.ts +++ b/lib/svg-icon.component.ts @@ -1,6 +1,8 @@ -import { Component, ElementRef, Input, OnInit, Optional, Renderer, SkipSelf } from '@angular/core'; +import { Component, ElementRef, Input, OnDestroy, OnInit, Optional, Renderer, SkipSelf } from '@angular/core'; import { Http } from '@angular/http'; +import { Subscription } from 'rxjs/Subscription'; + import { SvgIconRegistryService } from './svg-icon-registry.service'; @@ -10,23 +12,25 @@ import { SvgIconRegistryService } from './svg-icon-registry.service'; template: '' }) -export class SvgIconComponent implements OnInit { +export class SvgIconComponent implements OnInit, OnDestroy { @Input() src:string; + private icnSub:Subscription; + constructor(private element:ElementRef, private renderer:Renderer, private iconReg:SvgIconRegistryService) { } ngOnInit() { - this.loadSvg(); - } - - private loadSvg() { - this.iconReg.loadSvg(this.src).subscribe(svg => { + this.icnSub = this.iconReg.loadSvg(this.src).subscribe(svg => { this.setSvg(svg); }); } + ngOnDestroy() { + this.icnSub.unsubscribe(); + } + private setSvg(svg:SVGElement) { const icon = svg.cloneNode(true); let elem = this.element.nativeElement;