动态RouterLink href值正在变化但路由器链接正确

时间:2016-08-08 17:00:50

标签: angular angular2-routing

我有一个ng-for循环动态输出我的菜单类。第一次加载页面时,ng-reflect-router-link和ng-reflect-href设置正确。问题是在点击链接后,所有ng-reflect-hrefs都会更改为点击的链接。

routes.ts

import { RouterConfig } from '@angular/router';
import { Home } from './views/home/home';
import { FetchData } from './components/fetch-data/fetch-data';
import { Counter } from './components/counter/counter';

export const routes: RouterConfig = [

    { path: 'home', component: Home },
    { path: 'counter', component: Counter },
    { path: 'fetch-data', component: FetchData }
];

边栏-menu.ts

import * as ng from '@angular/core';
import {ROUTER_DIRECTIVES, Http} from './index.ts'

@ng.Component({
    selector: 'sidebar-menu',
    template: require('./sidebar-menu.html'),
    directives: [...ROUTER_DIRECTIVES]
})
export class SidebarMenu {
    public menus: Menu[];
    constructor(http: Http) {
        //todo - put this in a service.ts
        http.get('/api/Menu/Menus').subscribe(result => {
            this.menus = result.json();
        });
    }


}

menu.ts

interface Menu {
    name: string;
    decorator: string;
}

边栏-menu.html

<ul>    
<li *ngFor="#menu of menus" >
<a [routerLink]="menu.name">
            <span class='glyphicon glyphicon-th-list {{menu.decorator}}'></span> {{menu.name}}</a>
/li>
</ul>

点击链接后(couter是我在下面的示例中点击的链接),这就是html的样子:

<ul class="nav navbar-sidebar">

        <!--template bindings={
  "ng-reflect-ng-for-of": "[object Object],[object Object]"
}--><li>
            <a ng-reflect-router-link="counter" ng-reflect-href="/counter" href="/counter">
                <span ng-reflect-class-name="glyphicon glyphicon-th-list " class="glyphicon glyphicon-th-list " classname="glyphicon glyphicon-th-list "></span> counter
            </a>
        </li><li>
            <a ng-reflect-router-link="fetch-data" ng-reflect-href="/counter" href="/counter">
                <span ng-reflect-class-name="glyphicon glyphicon-th-list " class="glyphicon glyphicon-th-list " classname="glyphicon glyphicon-th-list "></span> fetch-data
            </a>
        </li>
        </ul>

我的项目设置与Dynamic routerLink value from ngFor item giving error "Got interpolation ({{}}) where expression was expected"非常相似。 但我的链接显示正确,不像那篇文章。

1 个答案:

答案 0 :(得分:0)

我的问题是我的json对象定义了比我的javascript界面​​更多的属性。

Menu object