2

我想将动态数据传递给材料表。我显示我的代码:

父组件.html

<div class="overview">
  <div class="vehicle-img">
    <img src={{vehicleImg}} />
  </div>
  <div class="board">
    <div class="board-column company-overview">
      <div class="board-column-header">{{ "COMPANY_OVERVIEW.TITLE" | translate}}</div>
      <div class="board-column-content">
        <app-company-overview [companyOverviewData]="companyOverviewData"></app-company-overview>
        <div *ngIf="!companyOverviewData" class="loading">
          <app-loading></app-loading>
        </div>
      </div>
    </div>
    <div class="board-column feeds">
      <div class="board-column-header">{{ "FEEDS.TITLE" | translate}}</div>
      <div class="board-column-content">
        <app-feeds [feedsOverviewData ]="feedsOverviewData "></app-feeds>
        <div *ngIf="!feedsData" class="loading">
            <app-loading></app-loading>
      </div>
    </div>
  </div>
</div>

在这个父组件中,它会调用一个 API 来获取 feedsOverviewData

 this.feedsService.getFeedsByVin(this.vin).subscribe((data) => {
        this.feedsOverviewData = data;
      });

并且这个 feedsOverviewData 将被传输到 app-feeds 组件,这个组件还有一个子组件 feeds-list 组件,

<div class="feeds">
  <mat-card class="feed-card">
    <mat-card-title>
      <div class="title">
        <h3>{{ 'FEEDS.SUBTITLE' | translate}} </h3>
        <hr>
      </div>
    </mat-card-title>
    <app-feed-list [feedsOverviewData]="feedsOverviewData"></app-feed-list>
    <div class="comment">
      <textarea class="textarea" matInput placeholder="Enter your comment ..." [(ngModel)]="feedsComment"></textarea>
      <button mat-button [disabled]="!feedsComment">
        <mat-icon class="send-button" matSuffix>send</mat-icon>
      </button>
    </div>
  </mat-card>
</div>

这个提要列表组件是用角度材料表实现的。

<table mat-table [dataSource]="dataSource">
    <ng-container matColumnDef="feeds">
      <td mat-cell *matCellDef="let item">
        <mat-card-header>
          <div mat-card-avatar>
            <mat-icon class="avatar">insert_emoticon</mat-icon>
          </div>
          <mat-card-title class="feeds-header"><b>
              <!-- <div *ngIf="item.type === 'searchevent'">
                            <span>n-Level {{item.user}}</span>
                        </div> -->
              <div *ngIf="item.comment === 'searchevent'">
                <span>Event</span>
              </div>
            </b>
          </mat-card-title>
          <mat-card-subtitle class="feeds-date">{{item.timestamp | date: 'dd/MM/yyyy'}}</mat-card-subtitle>
        </mat-card-header>
        <mat-card-content>
          <div *ngIf="item.type !== 'searchevent'">
            <div class="feeds-info">{{item.comment}}</div>
          </div>
          <div *ngIf="item.type === 'searchevent'">
            <div class="feeds-info">FIN search executed by {{item.user}}.</div>
          </div>
        </mat-card-content>
      </td>
    </ng-container>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>

而 feed-list.ts 就是这样的:

export class FeedListComponent implements OnInit {
  displayedColumns: string[] = ['feeds'];
  @Input() feedsOverviewData: Feeds[];
  @ViewChild(MatPaginator) paginator: MatPaginator;
  dataSource = new MatTableDataSource<any>();
  constructor() {
  }

  ngOnInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.data = this.feedsOverviewData;
  }
  ngOnChange() {
    console.log(this.feedsOverviewData);
    this.dataSource.data = this.feedsOverviewData;
  }

}

我尝试了很多方法来将此动态数据传递给表,但它不起作用。

你有什么想法?

此致,

狮子座

4

1 回答 1

2

你可以像这样使用它:基于数据的副本创建一个新的 DataSource(为了不变性)

this.dataSource = new MatTableDataSource([...<[whatever incoming data]>);

试试这个(对于你的情况)

this.feedsService.getFeedsByVin(this.vin).subscribe((data) => {
        this.feedsOverviewData = [...data];
      });

(ngOnInit 的类似设置)

 ngOnChange() {
    console.log(this.feedsOverviewData);
    this.dataSource = new MatTableDataSource(this.feedsOverviewData);
  }

订阅在其自己的异步进程中工作,数据仅在那里可用。Angular 在幕后做了一些事情来完成这项工作。

 <app-feed-list [feedsOverviewData]="this.feedsOverviewData"></app-feed-list>
于 2019-03-29T12:40:37.373 回答