OPV*_*OPV 2 angular angular7 angular8
我向服务器发出请求并获得 JSON 结果:
{"result" => "HTML code"}
Run Code Online (Sandbox Code Playgroud)
如何解析 HTML 代码并从此响应中获取表格?
我试图将此代码放在页面上的隐藏块中:
<div #content>HTML code here from response</div>
Run Code Online (Sandbox Code Playgroud)
接下来是什么?如何解析?
import { Pipe, PipeTransform } from "@angular/core";
import { DomSanitizer } from "@angular/platform-browser";
@Pipe({name: 'sanitizeHtml'})
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {
}
transform(value: string) {
return this.sanitized.bypassSecurityTrustHtml(value);
}
}
Usage:
<div [innerHTML]="content | sanitizeHtml"></div> //Content is what you got from json
Run Code Online (Sandbox Code Playgroud)
我们应该始终对内容进行消毒,以防止使用 DOMSanitizer 进行任何恶意活动。所以为此我们可以像上面一样创建一个管道并在应用程序的任何地方使用它。
小智 6
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 5';
demo: string = `<div>
<p>I am Yogesh</p>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</div>`
}
Run Code Online (Sandbox Code Playgroud)
应用程序组件.html
<div [innerHTML]="demo"></div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9455 次 |
| 最近记录: |