【问题标题】:When I retrieve data from the API but Its show as object in Angular当我从 API 检索数据但它在 Angular 中显示为对象时
【发布时间】:2021-02-26 10:11:36
【问题描述】:

当我从 API 检索数据时,会显示“名字”和“姓氏”值。但是“jobTitle”值没有显示在表格中,但它在控制台框中显示为一个数组。请帮我解决这个问题

这是 visibility.ts 文件:

export class VisibilityComponent implements OnInit {
  pmDetails1: IEmployee[];

  constructor(private userService: UserService) {}

  ngOnInit() {
    this.userService.getAllUsers().subscribe((pmDetails1: IEmployee[]) => {
      this.pmDetails1 = pmDetails1;
       console.log(pmDetails1);
       console.log(pmDetails1[0].jobTitle);
    });
  }
}

这是 HTML 文件

<div>
          <cdk-virtual-scroll-viewport itemSize="50" class="dialogbox-viewport">
            <table class="table table-light" style="border: 1px solid">
              <thead style="background-color: aqua">
                <tr>
                  <th>Select</th>
                  <th>Name</th>
                  <th>Role</th>
                </tr>
              </thead>

              <tbody>
                <tr *ngFor="let p of pmDetails1">
                  <td>
                    <input type="checkbox" value="{{p.firstName}}" (change) = "onSelectedEmployee($event)" [checked]="check" /> 
                  </td>
                  <td>{{ p.firstName }} {{ p.lastName }}</td>
                  <td>{{ p.jobTitle }}</td>
                </tr>
              </tbody>
            </table>
          </cdk-virtual-scroll-viewport>
        </div>

这是 IEmployee.ts 文件

export interface IEmployee {
    userId: any;
    firstName: string;
    jobTitle: any;
    lastName: String;
}

这是输出:

![在此处输入图片描述][1]

这里是“console.log(pmDetails1)”

![在此处输入图片描述][2]

这里是“console.log(pmDetails[0].jobTitles)”

我想在表格的 Role 列中显示 jobTitlesName。我是 Angular 的初学者。请帮我做

【问题讨论】:

  • 谁在后端创建该对象?根据您的图像,jobTitles 不是数组,而是具有自身多个属性的对象。那么你想如何为你的视图字符串化该对象呢?

标签: javascript angular typescript


【解决方案1】:

在您的 HTML 文件中使用它。

<td>{{ p.jobTitle.jobTitleName }}</td>

【讨论】:

    【解决方案2】:

    您的代码中一切正常,唯一的问题是属性jobTitleObject

    您有 3 个可用选项(或更多)

    选项 1

    从后端将值作为字符串返回; 这意味着我们可以将您的界面修改为

        export interface IEmployee {
            userId: any;
            firstName: string;
            jobTitle: string;
            lastName: String;
    
        }
    

    选项 2

    更改接口以从后端反映Object

        export interface IEmployee {
            userId: any;
            firstName: string;
            jobTitle: { jobTitleName: string; jobTitleId: number };
            lastName: String;
    
        }
    

    在您的 html 中,您现在可以如下使用它。 @shehanpathirathna 已经说明了这种方法

    <td>{{ p.jobTitle.jobTitleName }}</td>
    

    选项 3

    使用map 生成具有所需结构的新对象

        import { map } from 'rxjs/operators';
        export class VisibilityComponent implements OnInit {
          pmDetails1: IEmployee[];
    
          constructor(private userService: UserService) {}
    
          ngOnInit() {
            this.userService.getAllUsers().pipe(
               map(employeea => employees.map(
                  (employee) => ({...employee, jobTitle: jobTitle.jobTitleName })
               ))
            ).subscribe((pmDetails1: IEmployee[]) => {
              this.pmDetails1 = pmDetails1;
               console.log(pmDetails1);
               console.log(pmDetails1[0].jobTitle);
            });
          }
        }
    

    对于这种特定情况,此选项可能有点矫枉过正,但如果对象变得更复杂,它会非常有用

    【讨论】:

    • 我也在写同样的答案,但欧文开尔文把它贴在我面前。这是您面临的这个问题的完美解决方案。
    【解决方案3】:

    试试这个:

    export class VisibilityComponent implements OnInit {
      pmDetails1: IEmployee[];
      constructor(private userService: UserService) {}
      ngOnInit() {
        this.userService.getAllUsers().subscribe(data => {
          this.pmDetails1 = data;
           console.log(pmDetails1);
           console.log(pmDetails1[0].jobTitle);
        });
      }
    }
    

    【讨论】:

    • 什么?为什么?仅仅因为你把胡萝卜叫做香蕉,这并不会改变它的本质。
    • 在 HTML &lt;td&gt;{{p.jobTitle.jobTitleName}}&lt;/td&gt;
    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2022-10-19
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多