来自ngFor项的动态routerLink值,给出错误“期望表达的Got插值({{}})". [英] Dynamic routerLink value from ngFor item giving error "Got interpolation ({{}}) where expression was expected"

查看:61
本文介绍了来自ngFor项的动态routerLink值,给出错误“期望表达的Got插值({{}})".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据组件中动态项目集在指令中设置routerLink值.但是Angular2抛出了一个错误:

I'm trying to set the routerLink value in a directive based on a dynamic set of items from the component. But an error is being thrown from Angular2:

EXCEPTION: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected at column 1 in [ {{item.routerLink}} ] in AppHeader@5:40 ("
    <a *ngFor="let item of headerItems" [ERROR ->][routerLink]=" {{item.routerLink}} ">
      {{item.text}}
    </a>
"): Header@5:40

header.component.ts

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router-deprecated';

@Component({
  selector: 'app-header',
  templateUrl: './app/components/header/header.component.html',
  directives: [ROUTER_DIRECTIVES]
})
export class AppHeader {
  headerItems: Object[] = [];

  constructor() {
    this.headerItems.push(
      { classes: 'navLink', routerLink: ['/Home'], text: 'Home' }
    );
  }
}

header.component.html

<div id="HeaderRegion">
  <nav class="nav">
    <a *ngFor="let item of headerItems" [routerLink]=" {{item.routerLink}} ">
      {{item.text}}
    </a>
  </nav>
</div>

推荐答案

您不能将[]{{}}结合使用,无论是前者还是后者

You can't use [] combined with {{}} either the former or the later

[routerLink]="item.routerLink"

应该做你想做的事.

routerLink="{{item.routerLink}}"

item.routerLink.toString()绑定到routerLink属性.

这篇关于来自ngFor项的动态routerLink值,给出错误“期望表达的Got插值({{}})".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆