【发布时间】:2018-12-18 13:53:49
【问题描述】:
我想使用来自 Spring 应用程序的 GET 请求在 Angular 应用程序中显示 HashMap。我试过这个:
弹簧代码:
@GetMapping("gateways")
public ResponseEntity<?> getGateways() {
Map<Integer, String> list = new HashMap<>();
list.put(1, "Bogus");
return ok(list.put);
}
角度服务:
getContractGatewaysList(): Observable<Array<ContractGatewaysList>> {
return this.http.get<Array<ContractGatewaysList>>(environment.api.urls.contracts.getContractGateways);
}
角度组件:
gateways: ContractGatewaysList[];
this.contractService.getContractGatewaysList()
.subscribe(value => {
if (value != null) {
this.gateways = value;
}
});
界面:
export interface ContractGatewaysList {
id: number;
name: string;
}
HTML 代码:
<div class="form-group gateway">
<div class="input-group-prepend">
<label for="merchant_id">Gateway</label>
</div>
<select class="custom-select" name="gateway" [(ngModel)]="contract.gateway" id="gateway" required>
<option selected>Please Select...</option>
<option [value]="gateway.id" *ngFor="let gateway of gateways">{{ gateway.name }}</option>
</select>
</div>
但我得到空列表。转换 Java Hashmap 并在 Angular 下拉菜单中显示值的正确方法是什么?请给我看一下工作代码示例,因为我遇到了这个问题吗?
我收到此错误:
ERROR Error: Cannot find a differ supporting object '3873648042962238500' of type 'number'. NgFor only supports binding to Iterables such as Arrays.
at NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck (common.js:3152)
at checkAndUpdateDirectiveInline (core.js:9246)
at checkAndUpdateNodeInline (core.js:10507)
at checkAndUpdateNode (core.js:10469)
at debugCheckAndUpdateNode (core.js:11102)
at debugCheckDirectivesFn (core.js:11062)
at Object.eval [as updateDirectives] (ContractNewComponent.html:50)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:11054)
at checkAndUpdateView (core.js:10451)
at callViewAction (core.js:10692)
【问题讨论】:
-
在订阅中添加控制台日志并发布结果
-
在开发工具中我只能看到错误。我更新了帖子。
-
你能在 Angular 的订阅中得到响应吗?使用 console.log() 检查,如果你得到响应,请评论响应的 JSON 格式。
-
尝试更改您的后端,而不是
Map发送您在角度中所期望的相同类型的对象列表。 -
你能给我看一下代码示例吗?
标签: angular spring typescript angular6