Search code examples
angularangular-materialangular-material-5

Angular table material won't complie: Unexpected closing tag "ng-container"


I'm working n Angular project integrating angular material5.2.5: Here is my app.component.ts:

import { Component } from '@angular/core';
import {MatTableDataSource} from '@angular/material';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
displayedColumns = ['position', 'firstName', 'lastName', 'email'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
 }

  export interface Element {
   position: number;
   firstName: string;
   lastName: string;
   email: string;
     }

   const ELEMENT_DATA: Element[] = [
   {position: 1, firstName: 'John', lastName: 'Doe', email:       '[email protected]'},
    {position: 1, firstName: 'Mike', lastName: 'Hussey', email: '[email protected]'},
    {position: 1, firstName: 'Ricky', lastName: 'Hans', email: '[email protected]'},
     {position: 1, firstName: 'Martin', lastName: 'Kos', email: '[email protected]'},
     {position: 1, firstName: 'Tom', lastName: 'Paisa', email: '[email protected]'}
   ];

Here is my app.component.html:

 <mat-table #table [dataSource]="dataSource">
 <ng-container matColumnDef="position" >
 <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
 <mat-cell *matCellDef="let element">{{element.position}}</mat-cell>
 </ng-container>
 <ng-container matColumnDef="firstName">
 <mat-header-cell *matHeaderCellDef> First Name </mat-header-cell>
 <mat-cell *matCellDef="let element">{{element.firstName}}</mat-cell>
  </ng-container>
  <ng-container matColumnDef="lastName">
  <mat-header-cell *matHeaderCellDef> Last Name </mat-header-cell>
  <mat-cell *matCellDef="let element">{{element.lastName}}<mat-cell>
  </ng-container>
  <ng-container matColumnDef="email">
  <mat-header-cell *matHeaderCellDef> Email </mat-header-cell>
  <mat-cell *matCellDef="let element">{{element.email}}</mat-cell>
   </ng-container>
   <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    </mat-table>

Ps: I have configured the module for angular material. But I get this error: enter image description here

Any help, please?


Solution

  • Problem above is you have not closed <mat-cell> after the {{element.lastName}}. Here is the corrected code,

    Note: You could use AutoFormat on visual studio code to see the missing closing tags.

    <mat-table #table [dataSource]="dataSource">
      <ng-container matColumnDef="position">
        <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
        <mat-cell *matCellDef="let element">{{element.position}}</mat-cell>
      </ng-container>
      <ng-container matColumnDef="firstName">
        <mat-header-cell *matHeaderCellDef> First Name </mat-header-cell>
        <mat-cell *matCellDef="let element">{{element.firstName}}</mat-cell>
      </ng-container>
      <ng-container matColumnDef="lastName">
        <mat-header-cell *matHeaderCellDef> Last Name </mat-header-cell>
        <mat-cell *matCellDef="let element">{{element.lastName}}</mat-cell>
      </ng-container>
      <ng-container matColumnDef="email">
        <mat-header-cell *matHeaderCellDef> Email </mat-header-cell>
        <mat-cell *matCellDef="let element">{{element.email}}</mat-cell>
      </ng-container>
      <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
      <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    </mat-table>