2

I have a problem trying to bind the id of a route parameter to a variable.

The following error appears on the console:

EXCEPTION: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected at column 15 in [['/envio',{id:'{{text}}'}]] in AppComponent@3:11 ("
        <h1>Angular 2 Boilerplate</h1>
        <p>Hello World!</p>
        <a [ERROR ->][routerLink]="['/envio',{id:'{{text}}'}]">Go</a>
    "): AppComponent@3:11

The component that throws the exception:

import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES, RouteConfig, RouterLink} from "angular2/router";
import {EnvioIDComponent} from "./id.component";

@Component({
    selector: 'my-app',
    template: `
        <h1>Angular 2 Boilerplate</h1>
        <p>Hello World!</p>
        <a [routerLink]="['/envio',{id:'{{text}}'}]">Go</a>
    `,
    directives: [ROUTER_DIRECTIVES]    
})

@RouteConfig([
    {path: '/home', name: 'Inicio', component: AppComponent, useAsDefault: true},
    {path: '/envio/:id', name: 'EnvioID', component: EnvioIDComponent},

])

export class AppComponent {

    text:string='fine';
}

Thanks in advance!!

2 Answers 2

3

Simply use the variable directly:


<a [routerLink]="['EnvioID',{id:text'}]">Go</a>

A problem in your code is, you should use route name instead route path in routerLink.

Sign up to request clarification or add additional context in comments.

Comments

0

I made several changes in my code, including your solution Jiang YD, and now the following error appears:

Cannot resolve all parameters for 'Router'(RouteRegistry, Router, ?, Router).

Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Router' is decorated with Injectable.

The files of the project are:

boot.ts

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from "./app.component";
import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router} from "angular2/router";

bootstrap(AppComponent,[ROUTER_DIRECTIVES, ROUTER_PROVIDERS,Router]);

app.component.ts

import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES, RouteConfig, RouterLink, Router, ROUTER_PROVIDERS} from "angular2/router";
import {EnvioIDComponent} from "./id.component";

@Component({
    selector: 'my-app',
    template: `
        <h1>Angular 2 Boilerplate</h1>
        <p>Hello World!</p>
        <a [routerLink]="['EnvioID',{id:text}]">Go</a>
    `,
    providers: [ROUTER_PROVIDERS],
    directives: [ROUTER_DIRECTIVES]  
})

@RouteConfig([
    {path: '/envio/:id', name: 'EnvioID', component: EnvioIDComponent}
])

export class AppComponent {

    text:string='fine';
}

id.component.ts

import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES, RouteParams, RouteConfig, RouterLink, Router, ROUTER_PROVIDERS} from "angular2/router";
import {AppComponent} from "./app.component";

@Component({
    selector: 'envio',
    template: `
        <h1>Angular 2 Boilerplate</h1>
        <p>{{titulo}}{{id}}</p>
        <a [routerLink]="['Inicio']">Go back</a>
    `,
    providers: [ROUTER_PROVIDERS],
    directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
    {path: '/', name: 'Inicio', component: AppComponent, useAsDefault: true}
])

export class EnvioIDComponent {

    titulo:string='Success';
    id:string;

    constructor(private _params:RouteParams)
    {
        this.id=_params.get('id');
    }
}

5 Comments

seems your project not configured well, i can not run the plnkr project you post. please use official example as base https://angular.io/resources/live-examples/toh-6/ts/plnkr.html
I already compared my project to that sample and made several changes, but a lot of errors still showing up...I don't know what I'm doing wrong. plnkr.co/edit/ImicQGUYXtqgtggSOMyN?p=preview
I fixed most of these errors but now the link of the children page (Go back) doesn't work plnkr.co/edit/IifRRkNEJJfMKc4GHtMi?p=previewplnkr.co/edit/…
so bind a route parameter to a variable is solved right? please edit your question or ask another one.
Yes, the main question is already solved. I will ask a new one for that issue.Thanks for everything!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.