【问题标题】:Angular print a simple text in htmlAngular在html中打印一个简单的文本
【发布时间】:2018-10-25 12:31:15
【问题描述】:

我有这个 json

[
  {
    "id": 3,
    "name": "Test",
    "activityDays": 2,
    "coreBankingBranch": "master",
    "csrBranch": "master",
    "adminBranch": "master",
    "clientBranch": null,
    "creationDate": null,
    "serverName": "ATP",
    "cloneTag": null,
    "csrIsCompleted": false,
    "adminIsCompleted": false,
    "clientIsCompleted": false,
    "completed": false
  }
]

我从中得到的:

组件

 this.docker.getContainers().subscribe(containers => {
  console.log(containers);
});

服务

 getEnvironments() {
return this._http.get(this.baseUrl + "/environments", this.options);
}

我尝试使用

打印
<tr *ngFor="let environment of environments">
      <td>{{ environment.name }}</td>
    </tr>

但我明白了

EnvironmentComponent.html:79 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only

支持绑定到可迭代对象,例如数组。

【问题讨论】:

  • 您的环境阵列在哪里?您需要先执行以下操作:`getEnviroments() { this.enviroments = this._http.get(this.baseUrl + "/environments", this.options); }`
  • @user9793665 你的环境是数组类型吗?例如。 [{name:'name 1'}, {name:'name 2'}]
  • @user9793665 只需将环境数组绑定到订阅结果。 this.environments = containers

标签: javascript angular


【解决方案1】:

您似乎没有将environments 与响应绑定。还得在组件中发起environments

this.docker.getContainers().subscribe(containers => {
  this.environments = containers  // assuming container is the shared response 
});

【讨论】:

  • this.environments 是什么类型?我不想在前端和后端创建一个类。它可以是 Object[] 吗??
  • 如果您不确定,只需将其设为any 类型。您也可以跳过type,只需声明为空数组,如environments =[]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
  • 2017-02-10
相关资源
最近更新 更多