【发布时间】:2018-12-30 10:30:04
【问题描述】:
我正在尝试在命令的下拉列表中显示 json 键/字段;这是对象类型(见下面的json) 例如:“key1”、“key2”应该出现在下拉列表中。最初使用 *ngfor 但它给出了错误-“ngfor 仅支持绑定可迭代对象,例如数组”。 由于我的 json 不包含数组,因此尝试使用 ng-options 但无法填充下拉列表。我的 json 看起来像:
{
id: ‘bla’,
commands: {
“key1” : { },
“key2”: { }
}
}
html代码:
<select ngModel="selectedName" ng-options="cmd for cmd in cmdJson" name= "CapabilityCmd" required>
在打字稿代码中:
this.http.get(URL, options)
.pipe(map ((response) => response.json()))
.subscribe((res) => {
this.commandResponse = res;
this.cmdJson = this.commandResponse.commands;
console.log("commands:", this.cmdJson);
});
我注意到 cmdJson 显示了正确的响应(即;
{“key1” : { }, “key2”:{}}
) 在控制台中但不填充到下拉框中。
【问题讨论】:
标签: html angular typescript ng-options