Dynamic router-links
See original GitHub issueHey 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:
- Created 8 years ago
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
prefered method
without the router
Well… thank you! It works now but you got one
s
to much. The working example is: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!