我正在使用一个名为ag-grid
的库,它允许我创建一个数据表。在此表中,我通过routerLink
包含指向用户配置文件的链接。
我的问题是,如果用户在搜索中无效,该链接会将您带到无效的配置文件,因此我尝试使用不同的链接来处理该情况。
我创建了一个生成链接的组件,就像我需要它,但它只是将HTML打印到页面。
零件:
import { Component, OnInit } from '@angular/core';
@Component({
template: "{{ link }}"
})
export class ViewLinkComponent {
params: any;
link: any;
agInit(params: any): void {
this.params = params;
// If we don't have an NTID, we can assume this is an invalid record
if (this.params.data.QID == '-') {
// Loop over our data object
for (var key in this.params.data) {
if (this.params.data.hasOwnProperty(key)) {
// If we are not looking at order and our values are not empty
if (key != 'order' && this.params.data[key] != '-') {
this.link = this.createLink(this.params.data[key]);
return;
}
}
}
}else{
this.link = '<a routerLink="/profile/'+ params.data.NTID +'">View Profile</a>'
}
}
createLink(query){
return '<a href="https://external.com/search?q='+query+'" target="_blank">Search</a>';
}
}
最终结果:
下图显示了正确的链接,但未将其呈现为实际可点击链接。
但是,如果我要执行以下操作,它会很好:
@Component({
template: "<a routerLink='/profile/{{ params.data.NTID }}'>View Profile</a>"
})
问题在于我需要使用一些逻辑来确定它是使用路由还是外部链接的内部链接。
有没有办法让我告诉这个组件可以渲染HTML,假设它是安全的东西?
生成本机HTML代码的常用方法是Renderer2。但是,RouterLink
是一个指令,如果在运行时生成链接,则不会考虑它。你不能像这样绕过条件模板:
<a *ngIf="isUserValid" routerLink="/profile/...
<a *ngIf="!isUserValid" href="https://external.com/search...