Angular HTML属性未从Typescript传递到HTML
我是从Typescript发送的Angular HTML属性未从Typescript传递到HTML,angular,typescript,string-interpolation,Angular,Typescript,String Interpolation,我是从Typescript发送的 this.equationList = "<span id=\"Weight2\" class=\"weight\">W</span> = mg"; this.equationList=“W=mg”; 到HTML <div [innerHTML]="equationList"></div> 当我看到inspector中的元素时,我得到的是 <span class="weight">W</
this.equationList = "<span id=\"Weight2\" class=\"weight\">W</span> = mg";
this.equationList=“W=mg”;
到HTML
<div [innerHTML]="equationList"></div>
当我看到inspector中的元素时,我得到的是
<span class="weight">W</span> = mg
W=mg
id=“Weight2”无法通过。我尝试在span中发送其他属性。只有class=“weight”成功了。您是否尝试过父子关系?您可以使用@Outputs将数据从子级传递到父级,也可以使用@Inputs将数据从父级传递到子级 创建一个如下所示的管道:-
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';
@Pipe({
name: 'safe'
})
export class SafePipe implements PipeTransform {
constructor(protected sanitizer: DomSanitizer) {}
public transform(value: any, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
switch (type) {
case 'html': return this.sanitizer.bypassSecurityTrustHtml(value);
case 'style': return this.sanitizer.bypassSecurityTrustStyle(value);
case 'script': return this.sanitizer.bypassSecurityTrustScript(value);
case 'url': return this.sanitizer.bypassSecurityTrustUrl(value);
case 'resourceUrl': return this.sanitizer.bypassSecurityTrustResourceUrl(value);
default: throw new Error(`Invalid safe type specified: ${type}`);
}
}
}
使用您的innerHtml,如:-
<div [innerHTML]="equationList|safe: 'html'"></div>
您是否尝试过使用角度表达式
{{}