2017-01-09 53 views

回答

2

這就是後衛是https://angular.io/docs/ts/latest/guide/router.html#!#resolve-guard

import { Injectable }    from '@angular/core'; 
import { Router, Resolve, RouterStateSnapshot, 
     ActivatedRouteSnapshot } from '@angular/router'; 
import { Crisis, CrisisService } from './crisis.service'; 
@Injectable() 
export class CrisisDetailResolver implements Resolve<Crisis> { 
    constructor(private cs: CrisisService, private router: Router) {} 
    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<Crisis> { 
    let id = route.params['id']; 
    return this.cs.getCrisis(id).then(crisis => { 
     if (crisis) { 
     return crisis; 
     } else { // id not found 
     this.router.navigate(['/crisis-center']); 
     return null; 
     } 
    }); 
    } 
} 
1

你可以用與*ngIf所以模板,如果該條件被滿足您的模板將被創建:

<div *ngIf="students"> 
    <h1>And here is students list</h1> 

    <table> 
     <tr> 
     <th>Id</th> 
     <th>Name</th> 
     </tr> 
     <tr *ngFor="let student of students"> 
     <td>{{student.id}}</td> 
     <td>{{student.name}}</td> 
     </tr> 
    </table> 
    </div> 

修改過的翻頁遊戲:https://plnkr.co/edit/AhWQPBWZVm0vrZqHY6Jr?p=preview

+0

Thanx。這是簡單,簡單而有效的答案。 –

+0

@JamesJaved很高興我能幫到你。我建議你按照其他答案中提到的方式檢查解決後衛。 – echonax

1

你想使用Resolve-Guard。有關如何使用它們的完整說明,我建議你看看那裏。

您可以在顯示路由組件之前預取數據。請記住將您的警衛添加到您的提供者數組中。我有一次忘了。

相關問題