在Angular中使用innerHtml绑定HTML代码时,如果需要在innerHtml中使用锚标签,可以使用以下解决方法:
import { Component, OnInit, SecurityContext } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
safeHtml: SafeHtml;
constructor(private sanitizer: DomSanitizer) { }
ngOnInit() {
const html = 'Link';
this.safeHtml = this.sanitizer.sanitize(SecurityContext.HTML, html);
}
}
通过上述方法,你可以在Angular的innerHtml中使用锚标签,同时确保代码的安全性。