从GET JSON响应下拉列表中填充数据-Angular4

时间:2018-07-23 02:21:30

标签: html angular typescript ng-options

我正在尝试在命令下拉列表中显示json键/字段;这是对象类型(请参阅下面的json) 例如:“ key1”,“ key2”应出现在下拉列表中。最初使用* ngfor,但会出现错误-“ ngfor仅支持对可迭代对象(如数组)的绑定”。 由于我的json不包含Array,因此尝试在中使用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”:{}}

),但未填充到下拉框中。

1 个答案:

答案 0 :(得分:3)

angular中有 个ng选项,您使用的是angularjs语法,而不是使用 no

ngFor