Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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 角度材质MatTableDataSource从一个组件重定向到另一个组件(该组件也具有MatTableDataSource)后未更新_Angular_Angular Material_Angular Material Table - Fatal编程技术网

Angular 角度材质MatTableDataSource从一个组件重定向到另一个组件(该组件也具有MatTableDataSource)后未更新

Angular 角度材质MatTableDataSource从一个组件重定向到另一个组件(该组件也具有MatTableDataSource)后未更新,angular,angular-material,angular-material-table,Angular,Angular Material,Angular Material Table,我有两个角度模块: 学生 课程 在课程中,我有一个组件(CourseListComponent),我想在学生组件(StudentDetailsComponent)中嵌套显示它。因此,我需要从课程模块导出该组件: @NgModule({ declares [ CourseListComponent ], exports: [ CourseListComponent ] }) <pre> {{ dataSource.data | json }} </pre

我有两个角度模块:

  • 学生
  • 课程
在课程中,我有一个组件(CourseListComponent),我想在学生组件(StudentDetailsComponent)中嵌套显示它。因此,我需要从课程模块导出该组件:

@NgModule({
    declares [ CourseListComponent ],
    exports: [ CourseListComponent ]
})
<pre>
  {{ dataSource.data | json }}
</pre>
此外,我还需要在学生中导入课程模块:

@NgModule({
    declares: [ StudentListComponent, StudentDetailsComponent ],
    imports: [ CourseModule ]
})
在StudentListComponent中,我有一个MatTable with MatTableDataSource,其中包含一些数据:

this.students = this.route.snapshot.data.students;
this.dataSource = new MatTableDataSource(this.students);
当我切换到StudentDetails组件时,我希望显示该学生注册的课程列表。我还想在床垫内这样做:

this.courses = this.route.snapshot.data.courses;         // returns correct courses
this.dataSource = new MatTableDataSource(this.courses);  // changes filteredData
但是当页面加载时,它只显示空表。控制台中没有错误,只有一个空表

有人知道如何解决这个问题吗

谢谢大家!

以下是HTML:

<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8" matSort>
    <ng-container matColumnDef="id">
        <th mat-header-cell *matHeaderCellDef mat-sort-header class="aligned-columns">
            {{ 'COURSE-LIST.ID' | translate }}
        </th>
        <td mat-cell *matCellDef="let course"> 
            {{course.id}} 
       </td>
    </ng-container>
    <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>
            {{ 'COURSE-LIST.NAME' | translate }}
        </th>
        <td mat-cell *matCellDef="let course">
            {{ course.name}}
        </td>
    </ng-container>
    <ng-container matColumnDef="year">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>
            {{ 'COURSE-LIST.YEAR' | translate }}
        </th>
        <td mat-cell *matCellDef="let course"> 
            {{ course.year}} 
        </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
</table>
模型:

export inteface ICourse {
    id: number;
    name: string;
    year: string;
}

确保html是正确的。是否动态生成列?确保列名与“课程”字段相同。分配给课程后,您是否尝试打印此.datasource

首先做所有这些

还有一个问题,这些表是两个单独的表,在两个单独的组件中吗

试一试


如果它没有相同的结构,那就是问题所在。或者如果它为空,则表示您没有正确提供数据。

是。数据源是MatTableDataSource的一个实例,在赋值后具有正确的数据。但它并没有显示在表中。这些表格分为两个不同的部分。CourseListComponent属于课程模块,我有一个包含StudentListComponent和StudentDetailsComponent的学生模块。我试图在StudentDetailsComponent中显示CourseListComponent。它显示表格,但不显示数据。我将编辑我的问题以添加HTML。谢谢你的回答!我失踪了
。谢谢你的帮助!
<pre>
  {{ dataSource.data | json }}
</pre>
[
  {
    "id": 1,
    "name": "fasdfa",
    "year": 1992
  },
  {
    "id": 2,
    "name": "fasdf",
    "year": 2002
  },
  {
    "id": 3,
    "name": "Hydrfasdfogen",
    "year": 2034
  },
  {
    "id": 4,
    "name": "fas",
    "year": 2004
  },
  {
    "id": 5,
    "name": "afsdafasd",
    "year": 2014
  },
]