简体   繁体   English

Angular4绑定api数据查看不起作用?

[英]Angular4 bind api data to view is not working?

New to Angular2 and up ,I have data from backend that i am trying to bind it to view , it throws error dataSource does not exist , what is correct way to assign variable data to view in angular4 ? Angular2及更高版本的新手,我有后端试图将其绑定到视图的数据,它抛出错误dataSource不存在,将变量数据分配给angular4的正确方法是什么?

stream.component.ts stream.component.ts

@Component({
    selector: 'app-stream',
    templateUrl: './stream.component.html',
    styleUrls: ['./stream.component.css']
})
export class StreamComponent {
    title = 'From Stream Component';
    displayedColumns = ['ticketNum', "assetID", "severity", "riskIndex", "riskValue", "ticketOpened", "lastModifiedDate", "eventType"];
    data: any = [];
    dataSource: any = {};
    stream: any[];
    constructor(private streamService: StreamService) {
        this.getData();

    };

    getData() {
        this.streamService.getAllStream().subscribe(data => {
            this.data = data;
            dataSource = new MatTableDataSource(data);
            console.log("Stream", data);
            //let gridOptions = { data: 'stream' };
        });
    }

}
export interface Element {
    ticketNum: number;
    ticketOpened: number;
    eventType: string;
    riskIndex: string;
    riskValue: number;
    severity: string;
    lastModifiedDate: number;
    assetID: string;
}

stream.component.html stream.component.html

<mat-table #table [dataSource]="dataSource">

      <!-- Position Column -->
      <ng-container matColumnDef="ticketNum">
        <mat-header-cell *matHeaderCellDef> Ticket Number </mat-header-cell>
        <mat-cell *matCellDef="let element"> {{element.ticketNum}} </mat-cell>
      </ng-container>
</mat-table>

Error 错误

ERROR in src/app/stream/stream.component.ts(30,13): error TS2304: Cannot find name 'dataSource' . ERROR in src/app/stream/stream.component.ts(30,13): error TS2304: Cannot find name 'dataSource'

change dataSource = new MatTableDataSource(data); 更改dataSource = new MatTableDataSource(data); to this.dataSource = new MatTableDataSource(data); this.dataSource = new MatTableDataSource(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM