最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular routerLink - what is correct syntax when you want to add extra url text? - Stack Overflow

programmeradmin4浏览0评论

Hello I have a ponent that outputs links correctly with appropriate href url location (notice [routerLink]="card.id", http://localhost:4200/dashboard/my-cards/DmJHJJA1e77fEEImKBju):

        <a
            *ngFor="let card of cards; let i = index"
            class="list-group-item list-group-item-action d-flex flex-row align-items-start justify-content-between"
            [routerLink]="card.id"
            routerLinkActive="active"
        >
            <div class="d-flex flex-column card-info-container">
                <h4 class="list-group-item-heading">{{card.name}}</h4>
                <p class="mb-1">{{card.description}}</p>
            </div>
            <img [src]="card.imagePath" alt="" class="img-responsive" style="max-width: 50px;">
        </a>

I have another route set up like this:

{
    path: ':id/edit',
    ponent: CardEditComponent,
    canActivate: [AuthGuardService]
}

So what I am trying to do is in the ponent tag above is link directly to this route/ponent. I changed the [routerLink] syntax to what you see below and got this url http://localhost:4200/dashboard/my-cards/NaN

[routerLink]="card.id/edit"

It's clear that I cannot update the routerLink as I did - is this possible at all to do via syntax in the template only? Or must I have an onclick event to redirect as needed?

Hello I have a ponent that outputs links correctly with appropriate href url location (notice [routerLink]="card.id", http://localhost:4200/dashboard/my-cards/DmJHJJA1e77fEEImKBju):

        <a
            *ngFor="let card of cards; let i = index"
            class="list-group-item list-group-item-action d-flex flex-row align-items-start justify-content-between"
            [routerLink]="card.id"
            routerLinkActive="active"
        >
            <div class="d-flex flex-column card-info-container">
                <h4 class="list-group-item-heading">{{card.name}}</h4>
                <p class="mb-1">{{card.description}}</p>
            </div>
            <img [src]="card.imagePath" alt="" class="img-responsive" style="max-width: 50px;">
        </a>

I have another route set up like this:

{
    path: ':id/edit',
    ponent: CardEditComponent,
    canActivate: [AuthGuardService]
}

So what I am trying to do is in the ponent tag above is link directly to this route/ponent. I changed the [routerLink] syntax to what you see below and got this url http://localhost:4200/dashboard/my-cards/NaN

[routerLink]="card.id/edit"

It's clear that I cannot update the routerLink as I did - is this possible at all to do via syntax in the template only? Or must I have an onclick event to redirect as needed?

Share Improve this question asked Apr 14, 2018 at 19:59 Pippy LongstockingPippy Longstocking 3036 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

try routerLink = "{{card.id}}/edit"

You can set your routerLink as follows

[routerLink]="[card.id, 'edit']"

If you are passing route parameters then you need to have the route name first and then the params as ['routeName', parameter..]

But here since the route name itself includes the parameter, you need to concatenate both and use it as string.

[routerLink] = [ {{card. Id}} + '/edit']

发布评论

评论列表(0)

  1. 暂无评论