简体   繁体   English

绑定API Angular中的数据?

[英]Bind API Data in Angular?

I am trying to bind Data which is coming from API, Data is showing in the.network but When I am trying to bind data it is giving me an error -我正在尝试绑定来自 API 的数据,数据显示在 .network 中但是当我尝试绑定数据时它给我一个错误 -

Cannot find a differ supporting object
function'. NgFor only supports binding to Iterables such as Arrays.

API Data in Network API 网络数据

 [{deliveryid: 0, frequency: "", ContactName: "Aa", Email: "aa@we.com",…},…]
     
//While expanding the response

{deliveryid: 0, frequency: "", ContactName: "Aa", Email: "aa@we.com"}

.TS .TS

  public MyDigestEmailIdPrint = [];
  DigestEmailIdPrint() {
    var postData = {
    clientid: localStorage.getItem("storageselectedclient"),
  };

    this.article.DigestEmailIdPrint(postData).subscribe(
    (res) => { 
    if (res.message != "No Record Found") {
       this.MyDigestEmailIdPrint.push(res);
    }
  },
  (err) => {
    console.log(err);
  }
);

} }

.HTML .HTML

<tbody>
  <tr *ngFor="let details of MyDigestEmailIdPrint">
    <td> {{details.ContactName}}</td>
    <td> {{details.Email}}</td>
    <td> {{details.frequency}}</td>
    <td><input type="checkbox"></td>
  </tr>

</tbody> 

DigestEmailIdPrint is a function and doesn't return anything. DigestEmailIdPrint是 function 并且不返回任何内容。 Maybe you should use MyDigestEmailIdPrint in the ngFor but as pointed out in the comment, the data you get from the backend is not an array.也许你应该在MyDigestEmailIdPrint中使用ngFor但正如评论中指出的那样,你从后端获得的数据不是数组。

you can fix it like this:你可以这样修复它:

if (res.message != "No Record Found") {
    // use the spread syntax to create a new array and trigger change detection
    this.MyDigestEmailIdPrint = [...res.result];
}

I would suggest you to check the res type.我建议您检查res类型。 It seems it must be an object and thats what the error is pointing to NgFor only supports binding to Iterables such as Arrays.它似乎必须是 object,这就是错误指向NgFor only supports binding to Iterables such as Arrays.

The error is not only linked to this, the question is when are you calling the API and does the view load till that time?该错误不仅与此有关,问题是您什么时候调用 API 并且视图加载到那个时候?

Solution 1: If your response in the component is of different type, you can covert it to array.解决方案 1:如果您在组件中的响应是不同类型的,您可以将其转换为数组。

Solution 2: Check when the data is getting loaded into the view.解决方案 2:检查数据何时加载到视图中。 You should know the lifecycle hooks to get the data at correct time to get things in component.您应该知道生命周期挂钩,以便在正确的时间获取数据以获取组件中的内容。

Solution 3:解决方案 3:


if (res.message != "No Record Found") {
    // node need to push data to MyDigestEmailIdPrint
    // this.MyDigestEmailIdPrint.push(res); // add the data in the array
    this.MyDigestEmailIdPrint = res; // if res is an array it is fine to assign
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM