50

I'm trying to set the routerLink value in a directive based on a dynamic set of items from the component. But an error is being thrown from Angular2:

EXCEPTION: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected at column 1 in [ {{item.routerLink}} ] in AppHeader@5:40 ("
    <a *ngFor="let item of headerItems" [ERROR ->][routerLink]=" {{item.routerLink}} ">
      {{item.text}}
    </a>
"): Header@5:40

header.component.ts

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router-deprecated';

@Component({
  selector: 'app-header',
  templateUrl: './app/components/header/header.component.html',
  directives: [ROUTER_DIRECTIVES]
})
export class AppHeader {
  headerItems: Object[] = [];

  constructor() {
    this.headerItems.push(
      { classes: 'navLink', routerLink: ['/Home'], text: 'Home' }
    );
  }
}

header.component.html

<div id="HeaderRegion">
  <nav class="nav">
    <a *ngFor="let item of headerItems" [routerLink]=" {{item.routerLink}} ">
      {{item.text}}
    </a>
  </nav>
</div>
BenR
  • 2,791
  • 3
  • 26
  • 36
  • 1
    just had the issue, if solutions above give Can't bind to 'routerLink' since it isn't a known property of 'a'. and if your template is inside a custom module (like I did), dont forget to import RouterModule in your module too :-P – ninja May 03 '17 at 12:57

6 Answers6

125

You can't use [] combined with {{}} either the former or the later

[routerLink]="item.routerLink"

Should do what you want.

routerLink="{{item.routerLink}}"

would bind item.routerLink.toString() to the routerLink property.

Rap
  • 6,851
  • 3
  • 50
  • 88
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
17

You may also go with the following code to pass expression with some text.

<div *ngFor="let customer of customers">
   <a [routerLink]="'customer/'+customer.index">Link</a>
</div>
Pang
  • 9,564
  • 146
  • 81
  • 122
Manish Gupta
  • 276
  • 2
  • 5
5

You could have found the answer by this time, for me removing {{}} resolved the similar issue. Your code could be

<a *ngFor="let item of headerItems" [routerLink]="item.routerLink">
  {{item.text}}
</a>
madhav bitra
  • 155
  • 1
  • 3
5

Something like this worked for us:

<input type="checkbox" [id]="['btn.botStepState-'+i]" [(ngModel)]="btn.botStepState" name="btn.botStepState-{{i}}" (change)="changeHandler($event)" class="cbx hidden" />
  • Property binding i.e. [] required [] to evaluate values
  • Model binding i.e. [()] required nothing special
  • Interpolation i.e. {{}} could be used with general attributes
  • Event binding i.e. () worked great with functions
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • I fail to se how this is helpful, nothing in here is is related to the question outside of what a property binding is. – dudewad Apr 01 '18 at 21:48
  • @dudewad in my example, I'm showing how to use the synatx in general which applies to the question as well - so that the asker can take help from it. – Zameer Ansari Aug 23 '18 at 16:43
2

if you want to create router link by getting dynamic value from ngFor , you can do using below example

<div *ngFor = "let item of items"> 
<a [routerLink] = "['./item-details/' + item.id]">{{item.name}}</a></div>

that is , simply append the value you receive to the url you want to navigate.

Nikhil Kamani
  • 850
  • 9
  • 12
0

I have an example of how to make your routes dynamic

in your html component

<ul class="nav side-menu">
                <li *ngFor="let data of dataPermiso;let i= index">
                  <a><i class="{{data.icono}}"></i> {{data.categoria}} <span class="fa fa-chevron-down"></span></a>
                  <ul class="nav child_menu">
                    <li *ngFor="let item of data.permisoList;let a = index">
                      <a [routerLink]="item.ruta">{{item.titulo}}</a>
                    </li>
                  </ul>
                </li>
</ul>

in your component ts

  public dataPermiso:any[] = [];

  constructor(private loginserv: LoginService) {
    this.loadPermiso();
  }



  ngOnInit() {

  }

  public loadPermiso(){
    this.loginserv.listPermisos(this.dataStorage.idrol).subscribe((data)=>{
      this.dataPermiso= data;
    });
  }