2016-04-15 6 views
0

を注入:選択に基づいてangular2私はforループでオプションを生成<code>select</code>要素持つルータエラー

<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]を使用することを好むでしょうが、それは値として静的な文字列の解析のために動作していないようです。動的なナビゲーションでは、私はルータをコンストラクタに注入できないようです。以下はエラーです:

例外: 'ルータ'(RouteRegistry、Router、?、Router)のすべてのパラメータを解決できません。すべてのパラメータがInjectで装飾されているか、有効なタイプの注釈を持っていること、および 'Router'が注入可能で装飾されていることを確認してください。ここ

がコンポーネントである:

/// <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キーワードを削除しますが、_routernavigate(routeName)方法でアクセスできなくなります。

2. Angular2/coreからのインポートリストにInjectを追加しました。その後、コンストラクタを次のように変更しました。

constructor(@Inject() _router: Router) 
{ 
} 

まだ動作していません。どうすればこの問題を解決できますか?

providers: [Router] 

答えて

1

は、次の行を削除します
関連する問題