【发布时间】:2016-04-06 21:45:45
【问题描述】:
我已阅读this question about how to determine the active route,但我仍然不清楚如何使用参数确定活动路线?
现在我正在这样做:
<a [routerLink]="['/Profile/Feed', {username: username}]"
[ngClass]="{active: getLinkStyle('/profile/john_doe/feed')}">
Feed for {{username}}
</a>
在我的组件内部:
getLinkStyle(path:string):boolean {
console.log(this._location.path()); // logs: '/profile/john_doe/feed'
return this._location.path() === path;
}
这会起作用,因为我将用户名作为字符串传递。有没有办法通过传递正确的参数来做到这一点??
【问题讨论】:
-
您可以将 $routingPatams 注入您的控制器并从您的路由中获取参数。
-
是的,我知道,事实上
username来自 RouteParams。但是我怎样才能将它从我的 HTML 传递给我的组件呢? @CyberAleks -
我想要做的,类似于:
[ngClass]="{active: getLinkStyle('/profile/{{username}}/feed')}",但是这当然行不通.. -
你可以像 '/profile/'+username+'/feed' 那样做
-
是的,谢谢@CyberAleks。试过了,但是我的编辑器没有给它正确的语法颜色..所以没有测试它,愚蠢的哈哈!谢谢!
标签: angular angular2-template angular2-routing