2016-01-23 26 views
1

我正在使用Angular 2 beta(TypeScript)。頁面在Angular 2中不顯示完整

當我使用<a [routerLink]="['Friends']">Friends</a>去這個頁面點擊鏈接,它顯示,無論是文本好友列表:標識好。

然而,當我使用的代碼this._router.navigate(['/Friends']);去這個頁面,那隻能說明文字好友列表:,但沒有標識

import {Component, View} from 'angular2/core'; 

@Component({ 
    selector: 'friends-component' 
}) 
@View({ 
    template: ` 
     friends list: 
     <ul> 
      <li *ngFor="#friend of friends"> 
       {{friend.id}} 
      </li> 
     </ul> 
    ` 
}) 
export class FriendsComponent { 
    public friends = FRIENDS; 
} 

var FRIENDS: any[] = [ 
    { "id": 11 }, 
    { "id": 22 }, 
    { "id": 33 }, 
]; 

回答

1

的問題是,因爲我的代碼this._router.navigate(['/Friends']);運行外的角區。詳情請查詢this

解決的辦法是:

首先

import {NgZone} from 'angular2/core'; 

其次,添加此

constructor(private _ngZone: NgZone) {} 

三,修改代碼,

this._ngZone.run(() => { 
    this._router.navigate(['/Friends']); 
}); 
-1

您應該使用

this._router.parent.navigate(['/Friends']): 
+0

謝謝,但它並不能幫助,結果仍然相同。對於你的,我不知道'this._router.parent.navigate'和'this._router.navigate'之間的區別。但他們都工作。在這個問題下檢查[this comment](http://stackoverflow.com/questions/33668857/angular-2-im-trying-to-navigate-to-another-route-using-router-parent-navigate)。 –

+0

最好補充一點解釋爲什麼這是正確的解決方案。 –

+0

我認爲這是在Angular 2舊版本中的正確解決方案。對不起。但API變化很快。 – Bryan