在Angular 5中,点击锚点时构建一个动态URL

4
我有一个包含多个锚点标签的表格。我想在单击每个锚点标签时动态创建外部URL。我尝试使用[routerLink],但它会附加基本URL。是否有任何Angular的方法来实现?非常感谢您的帮助。
HTML
<ng-container matColumnDef="invoiceNo">
          <mat-header-cell *matHeaderCellDef > Invoice # </mat-header-cell>
          <mat-cell *matCellDef="let invoice"> 
            <a [routerLink]="getInvoiceUrl()"  target="_blank">
              {{invoice.invoiceNumber}}
            </a>
          </mat-cell>
        </ng-container>

ts

 getInvoiceUrl(){

return "www.google.com";

}


你好,JavaLearner。你找到解决方案了吗?我也有类似的问题。 - Ali Altun
4个回答

3

是的,只需要使用 href:

<a [href]="getInvoiceUrl()">

请确保包含http:否则将包括域名。

所以:

return "http://www.google.com";

一个问题是我想动态创建链接。所以当我在href中添加函数调用时,它会给我一个错误的响应错误。当我再次尝试将其添加到(click)事件中时,仍然存在错误...有什么建议吗?<a (click)="{{getInvoiceUrl(invoice.filePath)}}" target="_blank"> - DJ4186

2

公共链接="public"

在标签中使用 --> [href]=link> {{ link }}

它将重定向到 www.localhost:4200/public


1
如果您使用routerLink,则需要配置自己的路由,而在您的情况下,只需使用[href]即可。
<a href="{{getInvoiceUrl()}}">Link</a>

一个问题是我想动态创建链接。所以当我在href中添加函数调用时,它会给我一个错误的响应错误。当我再次尝试将其添加到(click)事件中时,也会出现错误.....有什么建议吗?<a (click)="{{getInvoiceUrl(invoice.filePath)}}" target="_blank"> - DJ4186

0

你能使用除了锚标签之外的其他东西吗?比如说:

<span (click)="takeMeToDynamicUrl()">link text</span>

然后您的方法可以创建URL并使用任何可用的方法将您带到URL,例如:

document.location.href = 'https://dynamicallycreatedurl.com';

这样,DOM节点失去了"<a>"标签的样式和行为。 - netalex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接