How to add routerlink with string interpolation in angular

Viewed 8716

I need to add

    router= "/update/id" in my code, where "id" has to be rendered dynamically based on values by *ngFor directive. (i.e) {{obj.id}}.So that id gets rendered dynamically

How to do this?

4 Answers

You can either use String Interpolation

routerLink="/update/{{obj.id}}"

or Attribute Binding Syntax:

[routerLink]="'/update/' + obj.id"

or as Pankaj suggested, Attribute Binding Syntax like this:

[routerLink]="['/update', obj.id]"

Component changes:

<a routerLink="/blog/{{blog.id}}">{{blog.title}}</a>

Module changes:

import { RouterModule } from '@angular/router';
imports: [
  RouterModule
]

If your component reside inside the feature module, you should also add the import and imports changes into the feature module

You could use

[routerLink]="['/update', obj.id]"
routerLink="{{objProperty}}/user/help" 

Working with Angular 7

Related