【发布时间】:2018-07-31 01:15:05
【问题描述】:
我在 html 端使用 *ngFor 对从 REST 服务接收的数据进行循环时遇到了一些问题,这就是我正在做的事情:
getQuestionaire(type: String) {
const url = `${this.questionaireTypeUrl}/${type}`;
return this.http.get(url).map(
(response: Response) => {const questioniares: any[] = response.json();
return questioniares;
}
);
}
这里我在其中一个 app.component.ts 中调用函数:
ngOnInit() {
this.route.params
.subscribe(
(params: Params) => {
this.quesType = params['type'];
this.questionaireService.getQuestionaire(this.quesType).subscribe(
(questionaires: any[]) => {
this.questionaire = questionaires;
console.log(this.questionaire);
}
);
}
);
}
这里我在 html 端使用 *ngFor 循环并动态创建表
<table class="table ">
<thead>
<tr>
<th class="active">QUESTIONAIRE NAME</th>
<th class="active">LEVEL</th>
<th class="active">LAST MODIFIED</th>
<th class="active">PUBLISHED BY</th>
<th class="active">VALIDITY DATE</th>
</tr>
</thead>
<tbody *ngFor="let questionaireEl of questionaire">
<tr>
<td>{{ questioniareEl.nameQuestionaire }}</td>
<td>{{ questionaireEl.levelQuestionaire }}</td>
<td>{{ questionaireEl.lastUser }}</td>
<td>{{ questionaireEl.publishingUser }}</td>
</tr>
</tbody>
</table>
问题:控制台记录了我循环的数据,它由一组对象组成,我不知道如何访问我需要显示的元素,请帮助!!
【问题讨论】:
-
你能显示来自服务器的 json 响应吗?
-
你需要
| async吗? -
你可以试试
{{ questioniareEl?.nameQuestionaire }}吗? -
在类定义中是否用空数组声明了 questioniares?如果不设置为 []。
-
@match 这两种解决方案我都尝试过并且它们都有效......但我想知道通配符运算符的作用?
标签: angular