Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track subscription for use in ngDestroy #18

Merged
merged 1 commit into from
Jun 1, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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