【发布时间】:2018-09-02 07:29:32
【问题描述】:
到目前为止,我只能在单击按钮 view 时加载所有 challenges:
这是可以理解的,因为在我的 view-one-challenge.component.html 中,我是这样编码的:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="modal-header" >
<h4 class="modal-title" >Challenge Details</h4>
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!-- HTML table for displaying a challenge details -->
<table class='table table-hover table-responsive table-bordered'>
<tr *ngFor="let challenge of challenge_list">
<td class="w-40-pct">Name</td>
<td>{{challenge?.name}}</td>
</tr>
<tr *ngFor="let challenge of challenge_list">
<td>Duration</td>
<td>{{challenge?.duration}}</td>
</tr>
<tr *ngFor="let challenge of challenge_list">
<td>Description</td>
<td>{{challenge?.description}}</td>
</tr>
<tr *ngFor="let challenge of challenge_list">
<td>Quiz</td>
<td>{{challenge?.Quiz.title}}</td>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="activeModal.close('Close click')">Close</button>
</div>
challenge_list 包含我通过challengeService 从我的 REST API 获得的所有挑战,以下是我用数据填充它的方式:
ngOnInit() {
console.log("inside ngOnInit...");
this.challengeService.getChallenges().subscribe(
data => {
this.challenge_list = data;
},
error => {
this.alertService.error(error);
});
}
从 REST API 获取数据的方法称为getChallenges(),通过 HttpClient 获取数据:
getChallenges(): Observable<Challenge[]> {
console.log("inside getChallenges ===============");
return this.http.get<Challenge[]>(this._challengeUrl);
}
我想做的是,我不想每次点击按钮view 时都加载所有挑战,我只想显示与被点击挑战的id相对应的数据。
很显然,我需要创建一个方法,该方法将 id:number 作为参数,遍历所有挑战并返回具有相应id 的挑战。但是,由于我对 Angular 有点陌生,所以我不知道该怎么做。任何想法甚至提示都将不胜感激。
另外,我认为我没有以优雅的方式加载数据,因为我第二次使用 challengeService 从 REST API 获取数据。有没有一种方法可以简单地将第一张图片(和另一个组件)中显示的数据“复制”或“使用”或“传输”或“注入”到我的弹出窗口中,而无需调用 API?提前谢谢各位。
【问题讨论】:
标签: html angular twitter-bootstrap typescript