Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 对象是填充html表时的数组,而不是在typescript中迭代时的数组_Angular_Typescript_Angular6_Primeng_Primeng Turbotable - Fatal编程技术网

Angular 对象是填充html表时的数组,而不是在typescript中迭代时的数组

Angular 对象是填充html表时的数组,而不是在typescript中迭代时的数组,angular,typescript,angular6,primeng,primeng-turbotable,Angular,Typescript,Angular6,Primeng,Primeng Turbotable,我有一个后端数据库,将学生记录作为JSON文件提供。然后将这些对象填充到预处理表中,以便显示和选择 student.component.html <p-table class="table" [columns]="cols" [value]="searchResults"> <ng-template pTemplate="header" let-columns> <tr> <th *ngFor="let c

我有一个后端数据库,将学生记录作为JSON文件提供。然后将这些对象填充到预处理表中,以便显示和选择

student.component.html

<p-table class="table" [columns]="cols" [value]="searchResults">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{col.header}}
            </th>
       </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData>
        <tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
            <td *ngFor="let col of columns">
                {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

那么,为什么素数表能够用对象填充表,但我不能在TypeScript中迭代它呢?如何使其将对象视为数组?

显然,
ts
文件中没有
声明

<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
    <td *ngFor="let col of columns">
        {{car[col.field]}}
    </td>
</tr>

发布错误消息,属性名称显示为.alert('1:'+JSON.stringify(this.searchResults));未定义,因为数据可能尚未解析。请记住,observable是异步的。请将此问题减少到解释问题所需的最低限度,包括错误消息并提供问题的详细说明。粘贴源代码并询问它为什么不起作用是离题的。很抱歉,在我写问题时,这是一个输入错误。因此,对检索到的数据起作用的每个方法(如我的
for
循环)都必须在subscribe函数内调用?为了确保它们仅在数据实际可用时执行?如果需要等待结果,则需要在subscribe函数中调用与工作相关的所有内容。如果这个答案对你有帮助,请考虑把它作为一个答案进行投票和/或标记。谢谢你的回答。我不熟悉角度,订阅和可观测的工作方式让我陷入了一个怪圈。
<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
    <td *ngFor="let col of columns">
        {{car[col.field]}}
    </td>
</tr>
<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
    <td *ngFor="let col of cols">
        {{car[col.field]}}
    </td>
</tr>
this.studentService.getStudents().subscribe(data => {
    this.searchResults = data;

     // table columns
    this.cols = [
      { field: 'studentId', header: 'Student ID'},
      { field: 'name', header: 'Name'},
      { field: 'dob', header: 'Date of Birth'},
      { field: 'status', header: 'Status'}
    ];

    // tested the object with these
    alert('1: ' + JSON.stringify(this.searchResults)); // undefined
    alert('2: ' + this.searchResults);                 // undefined
    alert('3: ' + this.searchResults.toString);        // undefined
});