Agregar condicionalmente routerLink u otras directivas de atributo a un elemento en Angular 2


En Angular 2 si tengo un elemento como <button></button> ¿cómo puedo agregar condicionalmente una directiva de atributos como [routerLink]="['SomeRoute'] a ella?

 23
Author: rcarrington, 2016-04-20

3 answers

Hasta donde yo sé no hay una manera directa de hacer esto. Hay algunas soluciones... Usé algo como esto:

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

Hay una discusión similar aquí: link

 14
Author: Boies Ioan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-20 20:09:41

O simplemente puede agregar una condición al atributo.

<button [routerLink]="myVar ? ['/myScreen'] : []"></button>

Redirige a '/MyScreen' solo si myVar es true.

 56
Author: fifix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-17 12:16:47
   <div [ngClass] ='{"disabled-link":!isMicrositeEnable,"cursor-pointer":"isMicrositeEnable"}' [routerLink]="isMicrositeEnable ? ['/microsite'] : []">
 1
Author: Ashutosh Jha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-02 08:17:38