我有一个select
元素,它通过for循环生成选项:
<select (change)="setSelectedSome($event.target.value)">
<option *ngFor="#some of Something" [selected]="SelectedSome == some" [value]="some.path" (click)="navigate(some.path)">{{some.name}}</option>
</select>
根据选择,我需要导航到一个组件。我更喜欢使用[routerLink]
,但由于解析静态字符串作为值,这似乎不起作用。因此,对于动态导航,我似乎无法将路由器注入构造函数。以下是错误:
异常:无法解析"Router"(RouteRegistry、Router、?、Router)的所有参数。请确保所有参数都用Inject修饰或具有有效的类型注释,并且"Router"用Injectable修饰。
这是组件:
/// <reference path="angular2/router.d.ts" />
import {Router, ROUTER_PROVIDERS, ROUTER_DIRECTIVES, LocationStrategy, HashLocationStrategy} from 'angular2/router';
@RouteConfig([
{ path: '/', name: 'Page', component: Page },
])
@Component({
providers: [Router],
directives: [ROUTER_DIRECTIVES],
selector: 'page',
template:
'<div>
<select (change)="setSelectedSome($event.target.value)">
<option *ngFor="#some of Something" [selected]="SelectedSome == some" [value]="some.path" (click)="navigate(some.path)">{{some.name}}</option>
</select>
<router-outlet></router-outlet>
</div>'
})
export class Page {
constructor(private _router: Router) { }
Something= [{path:'/Some', name: 'I'm Something!'];
SelectedSome = this.Something[0];
setSelectedSome(value) {
this.SelectedSome = value;
}
navigate(routeName) {
//Here i need to navigate, or if you can tell me how to use routerLink binding even better!
this._router.navigate(routeName);
}
}
bootstrap(Page, [ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy })]);
我尝试过的:
1。从构造函数中删除了private
关键字,但_router
在navigate(routeName)
方法中变得不可访问。
2。将Inject
添加到Angular2/core的进口列表中。然后将构造函数更改为以下内容:
constructor(@Inject() _router: Router)
{
}
仍然没有任何效果。我该怎么解决这个问题?
删除以下行:
providers: [Router]