有条件地将RouterLink或其他属性指令添加到Angular 2中的元素 [英] Conditionally add RouterLink or other attribute directives to an element in Angular 2

查看:90
本文介绍了有条件地将RouterLink或其他属性指令添加到Angular 2中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 2中,如果我有一个像<button></button>这样的元素,如何有条件地向其中添加一个像[routerLink]="['SomeRoute']这样的属性指令?

In Angular 2 if I have an element like <button></button> how can I conditionally add an attribute directive like [routerLink]="['SomeRoute'] to it?

推荐答案

据我所知,没有直接的方法可以做到这一点.有一些解决方法...我使用的是这样的:

As far as I know, there is no straight way to do this. There are some workarounds... I used something like this:

<button *ngIf="condition" [routerLink]="['SomeRoute']"></button>
<button *ngIf="!condition"></button>

这里有一个类似的讨论:链接

There is an similar discussion here: link

这篇关于有条件地将RouterLink或其他属性指令添加到Angular 2中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆