arrays - 如何处理 Angular 7 中数据不均匀的 JSON 对象?

标签 arrays json angular angular6 angular7

服务以这种格式返回数据。我可以在开发者工具栏中看到数据。

{"results":{"BindGridDatatable":[{"ID":"0005","Name":"Rohit"}, 
                             {"ID":"0006","Name":"Rahul"}], 
        "Totalvalue":119}}

在组件中我是这样接收的

 btnSearch(){ 

 this.service.postControlResult(this.myForm.value).subscribe(
  (res: Response)=> {
    this.data = res ;       
   },err => console.error(err)
 );
  }

在 HTML 文件中,我尝试了多种方式来显示我的数据,例如

<div class="table">
<div class="row" *ngFor="let d of data  ">
 {{d.BindGridDatatable.ID}}
</div>
</div>

<div class="table">
<div class="row" *ngFor="let d of data.BindGridDatatable   ">
 {{d}}
</div>
 </div>

我只想将 BindGridDatatable 绑定(bind)到 this.data 对象,我想将 Totalvalue 存储在单独的变量中。

我收到错误 TypeError: Cannot read property 'BindGridDatatable' of undefined

最佳答案

根据您的结果,您需要像这样迭代 -

<div class="table">
  <div class="row" *ngFor="let d of data?.results?.BindGridDatatable">
    {{d?.ID}} - {{d?.Name}} 
  </div>
</div>

PS: The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths.

并且主要用于异步调用数据绑定(bind),其中初始阶段变量值为 null 或未定义以避免此类问题,我们使用安全导航运算符 ?

有关的更多详细信息?

关于arrays - 如何处理 Angular 7 中数据不均匀的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54702976/

相关文章:

javascript - 表单为空时如何禁用提交按钮

angular - 如何自定义特定垫子徽章的颜色?

java - Java中将Json数组 "[{}]"转换为 "[]"

java - 使用简单 Json 库提取 JSON 数组

java - 如何将 byte[] 转换为 Byte[] 和其他方式?

json - JQ JSON 解析器,连接数组的某个子项

使用 Remote 的 JQuery 验证将空数据发布到 Web 服务

angular - react 形式 : how to pass a reference of a FormControl to its child so it can register a value change listener?

c - 游程代码解码中数组的奇怪行为

javascript - JavaScript 中的嵌套数组