question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic router-links

See original GitHub issue

Hey guys! I’m playing around with Typescript and Angular2 and wanted to create router-links with the help of ng-for with the help of an stringArray but had some problems… Following example:

import { Component, View, NgFor, bootstrap, bind} from 'angular2/angular2';
import { ROUTER_BINDINGS, LocationStrategy, HashLocationStrategy, RouteConfig, RouterOutlet, RouterLink} from 'angular2/router';
import { CompA } from 'scripts/compA';
import { CompB } from 'scripts/compB';

@Component({
  selector: 'app',
})
@View({
  template:
  `<template ng-for #button [ng-for-of]="buttons">
     <a [router-link]=['/{{buttons}}']  class="menuButtons">{{button}}</a>
   </template>
   <router-outlet></router-outlet>`,
  directives: [ RouterOutlet, RouterLink, NgFor]
})
@RouteConfig([
  { path: '/', redirectTo: '/compA' },
  { path: '/compA', component: CompA, as: 'compA' },
  { path: '/compB', component: CompB, as: 'compB' }
])
export class App {
  buttons: String[];
  constructor() { this.Buttons = ["compA", "compB"]; }
}
bootstrap(NavBar, [ROUTER_BINDINGS, bind(LocationStrategy).toClass(HashLocationStrategy)]);

When I try create the router-links like that i get the following error: EXCEPTION: Parser Error: Got interpolation ({{}}) where expression was expected at column 3 in [['/{{button}}']] in null

I worked around this issue with handling the href of each link myself changing to template lines to:

<template ng-for #button [ng-for-of]="buttons">
  <a href="/#/{{button}}" class="menuButtons" >{{button}}</a>
</template>
<router-outlet></router-outlet>`

While this is working I think it’s not the angular2 way of doing it. Can you help me somehow?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

14reactions
PatrickJScommented, Sep 6, 2015

prefered method

<a [router-link]=" [ '/' + buttons ] " class="menuButtons">{{ button }}</a>

without the router

<a [href]=" '/#/' + button " class="menuButtons" >{{ button }}</a>
1reaction
DeusProxcommented, Sep 6, 2015

Well… thank you! It works now but you got one s to much. The working example is:

<template ng-for #button [ng-for-of]="buttons">
  <a [router-link]=" [ '/' + button ]class="menuButtons" >{{button}}</a>
</template>
<router-outlet></router-outlet>

I couldn’t find any examples for this for hours and my own test-approaches had no success! Hope this can help other people too! Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic routerLink value from ngFor item giving error "Got ...
I'm trying to set the routerLink value in a directive based on a dynamic set of items from the component. But an error...
Read more >
Dynamic Router Link - StackBlitz
Starter project for Angular apps that exports to the Angular CLI.
Read more >
RouterLink - Angular
You can use dynamic values to generate the link. For a dynamic link, pass an array of path segments, followed by the params...
Read more >
Angular Pass Data to Route: Dynamic/Static - TekTutorialsHub
In this tutorial we will learn how to pass both static & dynamic data to the route. ... Using routerLink directive; Using navigateByUrl....
Read more >
Angular Dynamic Routes with Easy Example | by Pramod Patil
open article.component.html file and write following logic. On every button click dynamic type parameter is passed using routerLink. And added ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found