【发布时间】:2018-01-05 10:38:25
【问题描述】:
我有以下代码:
<select class="form-control" required>
<option *ngFor="let car of cars" type="text">{{car.Name}}</option>
</select>
我的问题是我只能得到一个名字,所以*ngFor 失败了。
HTML 中是否有办法制作类似的条件。如果不是 Javascript 或 Jquery
//There are more than 1 option
if(options.length>1){<option *ngFor="let car of cars" type="text">{{car.Name}}</option>}
//There is only an option
else {<option *ngIf="car" type="text">{{car.Name}}</option>}
汽车
export class Car {
id: String;
name: [{
brand: String,
}
}]
}
JSON 在有多个元素时返回Array[]。如果没有,它会返回一个Object{},所以我不能使用*ngFor
已解决 问题出在后端,而不是在前端
【问题讨论】:
-
问题不清楚
-
我已经编辑过了
-
您的原始代码看起来不错。如果集合中只有一辆车,那么您只会显示一个名称。
-
它是一个 JSON,如果它只是一个名称,它就不是一个数组,只是一个对象。所以我不能使用 ngFor
-
那么你不需要任何角度指令,只需要
{{cars.Name}}。如果cars只代表一辆汽车,则将其命名为car。
标签: html angular if-statement select