这是我的问题,我想在
HTML模板中包含一个链接到外部网站的按钮.但是,在阅读HTML时,Google Chrome会说:
WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).
似乎Angular 2的innerHTML属性不允许2个递归HTML绑定.
这是我的代码:
<ion-col width-80 innerHtml="{{ slide.content + '<p><button round full (click)=`'
+ slide.button.url +'`>'+slide.button.text+'</button>' }}"></ion-col>
删除(单击)属性,仅(slide.button.text)显示为单个文本.
有解决方案吗
幻灯片是这样的:
{
img: 'img/picture.png',
content: `sometext`,
button : {
url: `http://www.foo.com`,
text: `Site foo`
}
}
最佳答案 注意:我不知道离子的东西,所以改为HTML控件.将按钮更改为(链接)控件.
您可以使用DomSanitizationService,如下所示,
工作演示:http://plnkr.co/edit/y2BXvIO8egNxJPmM3j43?p=preview
//our root app component
import {Component, Pipe} from '@angular/core'
import {DomSanitizationService} from '@angular/platform-browser';
@Component({
selector: 'my-app',
template: `
<span [innerHTML]="myHTML"></span>
`,
})
export class AppComponent {
slide={
img: 'img/picture.png',
content: `sometext`,
button : {
url: `http://www.foo.com`,
text: `Site foo`
}
};
dangerousUrl='<p><a href='+`${this.slide.button.url}`+'>'+`${this.slide.button.text}`+'</a></p>';
constructor(sanitizer: DomSanitizationService) {
this.myHTML= sanitizer.bypassSecurityTrustHtml(this.dangerousUrl);
}
}