101

Using Angular 2, is there a setting to avoid adding prefix “unsafe:” to links. I need to set links for a protocol which is not whitelisted by default in Angular 2, but it is needed for our internal application, so the result is an invalid link:

    <a href="unsafe:Notes://MYSERVER/C1256D3B004057E8" ..

In older Angular there was compileProvider.aHrefSanitizationWhitelist, but I cannot find something similar in Angular 2.

1
  • I just had a whitespace in front of my URI Feb 26, 2018 at 14:36

2 Answers 2

201

Use the DomSanitizer:

import {DomSanitizer} from '@angular/platform-browser';
...
constructor(private sanitizer:DomSanitizer){}
...
let sanitizedUrl = this.sanitizer.bypassSecurityTrustUrl('Notes://MYSERVER/C1256D3B004057E8');

or create a method to return the sanitized url:

sanitize(url:string){
    return this.sanitizer.bypassSecurityTrustUrl(url);
}

and then in your template:

<a [href]="sanitize('Notes://MYSERVER/C1256D3B004057E8')" ..

Demo Plunk

11
  • 1
    Veri precise and complete instruction. Works perfectly. I wonder where you found this.... :)
    – Veljac
    May 25, 2016 at 18:24
  • Currently it's in the very top of angular2 change log page. It's written for style sanitizing, but once you have DomSanitizationService the rest is just a matter of a ctrl+space :D May 25, 2016 at 18:37
  • 6
    make sure to use property binding <a [href]="sanitize(x)"> and not insertion <a href="{{sanitize(x)}}">
    – r3mark
    Jul 4, 2017 at 1:46
  • 2
    Or because we're in JavaScript, simply: sanitize = this.sanitizer.bypassSecurityTrustUrl will work
    – A T
    Oct 9, 2017 at 0:42
  • 1
    make sure an url is safe before using bypassSecurityTrustUrl Dec 19, 2019 at 17:01
55

Another way is you can create a pipe service to change an unsafe URL to a safe URL, so there isn't any need to rewrite the code in all components. Create a pipe service called safe-url.pipe.ts:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({
  name: 'safeUrl'
})
export class SafeUrlPipe implements PipeTransform {
  constructor(private domSanitizer: DomSanitizer) {}
  transform(url) {
    return this.domSanitizer.bypassSecurityTrustResourceUrl(url);
  }
}

Then use it in your view.

Example:<a [href]="'Notes://MYSERVER/C1256D3B004057E8' | safeUrl"></a>

NOTE: Don't forget to inject this pipe service in your app.module.ts file:

import { SafeUrlPipe } from './shared/safe-url.pipe'; // Make sure your safe-url.pipe.ts file path is matching.

@NgModule({ declarations: [SafeUrlPipe],...});
4
  • 6
    Perhaps, need put the 'href' into square brackets - [href], so Angular could execute it.
    – Alex Klaus
    Feb 20, 2018 at 5:06
  • What is "node" (at the end) in this context? Node.js? Something in Angular? Something else? Jan 13, 2021 at 20:06
  • @PeterMortensen he might meant to say "Angular Module". So in your app.module.ts file.
    – muuvmuuv
    Aug 3, 2021 at 9:33
  • This is the right way of doing it, with a pipe. You can add pure: true, so it can only trigger when the value changes. Jan 20 at 14:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.