Angular2及以上的新手,我有来自后端的数据,我试图将它绑定到视图,它抛出错误dataSource
不存在,在angular4中分配变量数据的正确方法是什么? / p>
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
<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 in src/app/stream/stream.component.ts(30,13): error TS2304: Cannot find name 'dataSource'
。
答案 0 :(得分:0)
将dataSource = new MatTableDataSource(data);
更改为this.dataSource = new MatTableDataSource(data);