我正在嘗試爲Angular 2應用程序使用異步管道。角度異步管道,數據未填充模板
下面是不同的服務/組件的代碼,
服務:
export class WorkoutService {
constructor(public http: Http) { }
getExercises() {
return this.http.get(this.collectionsUrl + '/exercises' + this.params)
.map((res: Response) => <Exercise[]>res.json())
.catch(WorkoutService.handleError);
}
}
LeftNavExercisesComponent:
export class LeftNavExercisesComponent implements OnInit {
public exerciseList: Observable<Exercise[]>;
constructor(
public workoutService: WorkoutService,
public workoutBuilderService: WorkoutBuilderService) { }
ngOnInit() {
this.exerciseList = this.workoutService.getExercises();
//this.exerciseList.subscribe(data =>
// console.log(data));
}
HTML:
<div *ngIf='exerciseList && exerciseList.length > 0'>
<div *ngFor="let exercise of exerciseList|async|orderBy:'title'">
<button class="btn btn-info col-sm-12" (click)="addExercise(exercise)">{{exercise.title}}<span class="glyphicon glyphicon-chevron-right"></span></button>
</div>
</div>
雖然我使用下面的代碼,我能看到,API調用發生,這是給我回的結果,
this.exerciseList.subscribe(data => console.log(data));
但是,存在的情況下沒有API調用,
this.exerciseList = this.workoutService.getExercises();
我在html模板中應用'異步'管道,但似乎這是行不通的。
可能是什麼原因?謝謝!
你試過去除'orderBy'管道嗎? – Jai