代码之家  ›  专栏  ›  技术社区  ›  SBB

角度渲染链接

  •  1
  • SBB  · 技术社区  · 6 年前

    我正在使用一个名为 ag grid 的库,它允许我创建一个数据表。在这个表中,我通过一个 routerlink包括一个到用户配置文件的链接。

    我的问题是,如果用户在搜索中无效,链接会将您带到一个无效的配置文件,因此我正尝试为该情况使用其他链接。

    我创建了一个生成链接的组件,就像我需要它一样,但它只是将HTML打印到页面上。

    组件:

    import component,oninit from'@angular/core';
    
    @组件({
    模板:“链接”
    })
    
    导出类ViewLinkComponent{
    
    参数:任意;
    链接:任意;
    
    Aginit(参数:any):空{
    
    this.params=参数;
    
    //如果没有ntid,我们可以假定这是一个无效记录
    if(this.params.data.qid='-'){
    
    //循环我们的数据对象
    for(此.params.data中的var键){
    
    if(this.params.data.hasownProperty(key))。{
    //如果我们不查看顺序,并且我们的值不为空
    如果(键!='订单'&&this.params.data[key]!=‘-’){
    this.link=this.createLink(this.params.data[key]);
    返回;
    }
    
    }
    
    }
    
    }其他{
    this.link='<a routerlink=“/profile/'+params.data.ntid+'”>查看配置文件</a>'
    }
    
    }
    
    CreateLink(查询){
    返回'<a href=“https://external.com/search?”q='+query+'“target=”_blank“>搜索</a>';
    }
    
    }
    

    最终结果:

    下图显示了正确的链接,但没有将其呈现为实际的可单击链接。

    但是,如果我这样做,它会变得很好:

    组件({ 模板:“<a routerlink='/profile/params.data.ntid'>查看配置文件</a>” })

    问题在于,我需要使用一些逻辑来确定它是使用路由的内部链接还是使用外部链接的内部链接。

    有没有一种方法可以让我告诉这个组件可以呈现HTML,假设它是安全的?.

    我的问题是,如果用户在搜索中无效,那么链接会将您带到一个无效的配置文件,因此我正尝试为这种情况使用其他链接。

    我创建了一个生成链接的组件,就像我需要它一样,但它只是将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>';
      }
    
    }
    

    最终结果:

    下图显示了正确的链接,但没有将其显示为实际的可单击链接。

    enter image description here

    但是,如果我这样做,就会很好:

    @Component({
      template: "<a routerLink='/profile/{{ params.data.NTID }}'>View Profile</a>"
    })
    

    问题在于,我需要使用一些逻辑来确定它是使用路由的内部链接还是使用外部链接的。

    有没有一种方法可以让我告诉这个组件可以呈现HTML,假设它是安全的?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Guerric P    6 年前

    生成本机HTML代码的常见方法是 Renderer2 .然而, RouterLink 是一个指令,如果在运行时生成链接,则不会考虑它。 不能这样绕过条件模板:

    <a *ngIf="isUserValid" routerLink="/profile/...
    <a *ngIf="!isUserValid" href="https://external.com/search...