摘要:本文讲解了angular2显示后端返回的html 安全转换的方法,希望阅读本篇文章以后大家有所收获,帮助大家HTML+CSS入门。
1、上传文件回显缩略图
<input type="file" name="fileUpload" [(ngModel)]="fileUpload"
(change)="sendImg($event)"/>
sendImg(event) {
let url = window.URL.createObjectURL(event.srcElement.files[0]);
}// 获取到一个不安全的临时图片链接地址
2、在angular2中使用,定义管道,过滤
import { Pipe,PipeTransform, SecurityContext } from '@angular/core';import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';@Pipe({
name: 'safe'
})export class SafePipe implements PipeTransform {
constructor(protected dom: DomSanitizer) {}
public transform(value: string, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
switch (type) {
case 'html':
return this.dom.bypassSecurityTrustHtml(value);
case 'style':
return this.dom.bypassSecurityTrustStyle(value);
case 'script':
return this.dom.bypassSecurityTrustScript(value);
case 'url':
return this.dom.bypassSecurityTrustUrl(value);
case 'resourceUrl':
return this.dom.bypassSecurityTrustResourceUrl(value);
default:
return value;
}
}
}
使用
<div [innerHTML]="html | safe:'html'"></div>
本文由职坐标整理并发布,了解更多内容,请关注职坐标WEB前端HTML/CSS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号