Skip to content

Commit

Permalink
Merge pull request #18 from czeckd/ngdestroy
Browse files Browse the repository at this point in the history
Track subscription for use in ngDestroy
  • Loading branch information
czeckd authored Jun 1, 2017
2 parents 49ed097 + 78fce35 commit a79a8b2
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/svg-icon.component.ts
Original file line number Diff line number Diff line change
@@ -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';


Expand All @@ -10,23 +12,25 @@ import { SvgIconRegistryService } from './svg-icon-registry.service';
template: '<ng-content></ng-content>'
})

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 = <SVGElement>svg.cloneNode(true);
let elem = this.element.nativeElement;
Expand Down

0 comments on commit a79a8b2

Please sign in to comment.