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 在角材料中使用项目索引_Angular_Typescript_Angular Material - Fatal编程技术网

Angular 在角材料中使用项目索引

Angular 在角材料中使用项目索引,angular,typescript,angular-material,Angular,Typescript,Angular Material,我想找到角材料表中元素的索引。我写这段代码: <table mat-table matSort [dataSource]="dataSource" multiTemplateDataRows> <ng-container matColumnDef="title"> <th mat-header-cell *matHeaderCellDef> {{ 'GENERAL.TITLE' | tran

我想找到角材料表中元素的索引。我写这段代码:

<table mat-table matSort [dataSource]="dataSource" multiTemplateDataRows>


    <ng-container matColumnDef="title">

        <th mat-header-cell *matHeaderCellDef> {{ 'GENERAL.TITLE' | translate }} </th>
        <td mat-cell *matCellDef="let element ; let i = index">
            <span class="icon" *ngIf="element.isHeading==true">
                <mat-icon *ngIf="element.parentId==null" (click)="openChild(element.id,i)">keyboard_arrow_down</mat-icon>
            </span> {{element.title}} </td>
    </ng-container>


    <!-- <ng-container matColumnDef="courseTitle">
        <th mat-header-cell *matHeaderCellDef> {{ 'LESSON.COURSE_TITLE' | translate }} </th>
        <td mat-cell *matCellDef="let element" ktIsEllipsisActive> {{element.courseTitle }} </td>



    <ng-container matColumnDef="courseTitle">
        <th mat-header-cell *matHeaderCellDef> {{ 'LESSON.CLASS' | translate }} </th>
        <td mat-cell *matCellDef="let element" ktIsEllipsisActive> {{element.courseTitle }} </td>
    </ng-container>


    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row;let i=index; columns: displayedColumns;"
        [ngClass]="'child'+row.parentId"
        [class.isChild]="row.parentId!==null && row.isHeading===false "></tr>

</table>
但每次它显示我是
未定义的


如何访问
中项目的索引?

您的迭代器不完整。您正在未完成的for…of循环中声明一个元素变量

*matCellDef="let element ; let i = index"
*matCellDef="let element of yourArrayHere; let i = index"
在“element”变量后面应该有关键字“of”。在“of”之后,应该有对集合(例如数组)的引用。否则,元素变量将无处获取信息

如果要通过将表单元格绑定到typescript文件中的数组来呈现表单元格,请确保将该数组的名称添加到for…of循环的末尾

*matCellDef="let element ; let i = index"
*matCellDef="let element of yourArrayHere; let i = index"

@yazan我编辑了这个问题,你能提供关于ts文件中元素的详细信息吗?你会在这里找到答案,不确定,但在这里似乎效果不错: