Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

387
Views
Angular2 SVG xlink:href

I have component for rendering SVG icon:

import {Component, Directive} from 'angular2/core';
import {COMMON_DIRECTIVES} from 'angular2/common';

@Component({
  selector: '[icon]',
  directives: [COMMON_DIRECTIVES],
  template: `<svg role="img" class="o-icon o-icon--large">
                <use [xlink:href]="iconHref"></use>
              </svg>{{ innerText }}`
})
export class Icon {
  iconHref: string = 'icons/icons.svg#menu-dashboard';
  innerText: string = 'Dashboard';
}

This triggers error:

EXCEPTION: Template parse errors:
  Can't bind to 'xlink:href' since it isn't a known native property ("<svg role="img" class="o-icon o-icon--large">
<use [ERROR ->][xlink:href]=iconHref></use>
  </svg>{{ innerText }}"): SvgIcon@1:21

How do I set dynamic xlink:href?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

SVG elements doen't have properties, therefore attribute binding is required most of the time (see also Properties and Attributes in HTML).

For attribute binding you need

<use [attr.xlink:href]="iconHref">

or

<use attr.xlink:href="{{iconHref}}">

Update

Sanitization might cause issues.

See also

  • https://github.com/angular/angular/issues/9510)
  • https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizationService-class.html

Update DomSanitizationService is going to be renamed to DomSanitizer in RC.6

Update this should be fixed

but there is an open issue to support this for namespaced attributes https://github.com/angular/angular/pull/6363/files

As work-around add an additional

xlink:href=""

Angular can update the attribute but has issues with adding.

If xlink:href is actually a property then your syntax should work after the PR was added as well.

over 4 years ago · Santiago Trujillo Report

0

I was still having issues with the attr.xlink:href described by Gunter so I created a directive that is similar to SVG 4 Everybody but is specific for angular2.

Usage

<div [useLoader]="'icons/icons.svg#menu-dashboard'"></div>

Explanation

This directive will

  1. Load icons/icons.svg over http
  2. Parse the response and extract path info for #menu-dashboard
  3. Add the parsed svg icon to the html

Code

import { Directive, Input, ElementRef, OnChanges } from '@angular/core';
import { Http } from '@angular/http';

// Extract necessary symbol information
// Return text of specified svg
const extractSymbol = (svg, name) => {
    return svg.split('<symbol')
    .filter((def: string) => def.includes(name))
    .map((def) => def.split('</symbol>')[0])
    .map((def) => '<svg ' + def + '</svg>')
}

@Directive({
    selector: '[useLoader]'
})
export class UseLoaderDirective implements OnChanges {

    @Input() useLoader: string;

    constructor (
        private element: ElementRef,
        private http: Http
    ) {}

    ngOnChanges (values) {
        if (
            values.useLoader.currentValue &&
            values.useLoader.currentValue.includes('#')
        ) {
            // The resource url of the svg
            const src  = values.useLoader.currentValue.split('#')[0];
            // The id of the symbol definition
            const name = values.useLoader.currentValue.split('#')[1];

            // Load the src
            // Extract interested svg
            // Add svg to the element
            this.http.get(src)
            .map(res => res.text())
            .map(svg => extractSymbol(svg, name))
            .toPromise()
            .then(svg => this.element.nativeElement.innerHTML = svg)
            .catch(err => console.log(err))
        }
    }
}
over 4 years ago · Santiago Trujillo Report

0

I think it can be solved using angular pipe feature.

<use attr.xlink:href={{weatherData.currently.icon|iconpipe}}></use>

Explanation This pipe will

  1. Pass the parsed response value to the pipe
  2. Return the complete svg path
  3. attr.xlink:href= will get the expected path & svg will rendered in html page

here is pipe typescript code

import { PipeTransform, Pipe } from '@angular/core';

@Pipe({
    name: 'iconpipe'
})
export class IconPipe implements PipeTransform {
    constructor() { }

    transform(value: any) {
        let properIconName = undefined;

        switch (value) {
            case 'clear-day':
                properIconName = '/assets/images/weather-SVG-sprite.svg#sun';
                break;
            case 'clear-night':
                properIconName = '/assets/images/weather-SVG-sprite.svg#night-1';
                break;
            case 'partly-cloudy-day':
                properIconName = '/assets/images/weather-SVG-sprite.svg#cloudy';
                break;

            case 'partly-cloudy-night':
                properIconName = '/assets/images/weather-SVG-sprite.svg#night';
                break;

            case 'cloudy':
                properIconName = '/assets/images/weather-SVG-sprite.svg#cloud';
                break;

            case 'rain':
                properIconName = '/assets/images/weather-SVG-sprite.svg#rain';
                break;

            case 'sleet':
                properIconName = '/assets/images/weather-SVG-sprite.svg#snowflake';
                break;
            case 'snow':
                properIconName = '/assets/images/weather-SVG-sprite.svg#snowing';
                break;

            case 'wind':
                properIconName = '/assets/images/weather-SVG-sprite.svg#storm';
                break;
            case 'fog':
                properIconName = '/assets/images/weather-SVG-sprite.svg#sun';
                break;
            case 'humid':
                properIconName = '/assets/images/weather-SVG-sprite.svg#sun';
                break;
            default:
                properIconName = '/assets/images/weather-SVG-sprite.svg#summer';
        }
        return properIconName;
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!