Angular DataTables表中没有可用数据-新绑定

问题描述 投票:0回答:2

你能告诉我我在哪里犯错了吗?快速说明:我想使用此Angular DataTables,但与我将数据存储在汽车阵列中不同。

   cars$: Observable<Car[]>

   constructor(private carService: CarService) { }

   getCars() {
      this.cars$ = this.carService.getCars();
   }

   ngOnInit() {
      this.getCars();   
      this.dtOptions = {
         pagingType: 'full_numbers',
         pageLength: 2
       };   
   }

和此处html

<h3>Cars</h3>
  <table datatable [dtOptions]="dtOptions" class="row-border hover">
    <tfoot>
      <tr>
        <th><input type="text" placeholder="Search ID" name="search-id"/></th>
        <th><input type="text" placeholder="Search first name" name="search-first-name"/></th>
        <th><input type="text" placeholder="Search last name" name="search-last-name"/></th>
      </tr>
    </tfoot>
  </table>
  <table datatable class="row-border hover">
    <thead>
        <tr>
          <th>ID</th>
          <th>Name</th>
          <th>Mark</th>
          <th>Type</th>
          <th>Year</th>
          <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let car of cars$ | async">
            <th>{{ car.id }}</th>
            <th>{{ car.name }}</th>
            <th>{{ car.mark }}</th>
            <th>{{ car.type }}</th>
            <th>{{ car.year }}</th>
            <th>{{ car.description }}</th>
        </tr>
    </tbody>
  </table>

并且我得到数据,但是它们在此网格及其消息之后加载-没有可用数据,当然网格无法正常工作enter image description here

angular angular-datatables
2个回答
1
投票

在创建表之前,您应该等待可观察结果,因此您应该使用ng-container这样的形式:


0
投票

Observables应该订阅以显示流值:

© www.soinside.com 2019 - 2024. All rights reserved.