目前,我可以打电话给一个服务,并填写一个表的记录;记录具有选定的属性,该属性由每行的复选框切换。Angular2无法读取未定义的属性“推”
该表下面是一个按钮;当我点击按钮时,我想循环遍历表并创建一个新的数组,其条目已经选择了属性== true。我不能正确初始化selecterecords数组。
Record.ts
export class Record {
id: string;
firstname: string;
lastname: string;
salary: string;
selected : boolean;
}
表component.ts
export class TableComponent implements OnInit {
records: Record[];
selectedrecords: Record[];
constructor(
private router: Router,
private recordService: RecordService) {}
getRecords(): void {
this.recordService.getRecords().then(records => this.records = records);
}
ngOnInit(): void {
this.getRecords();
}
createselectedRecords() {
for (let rec of this.records) {
if (rec.selected) {
this.selectedrecords.push(rec);
}
console.log(this.selectedrecords);
}
}
}
记录service.ts
@Injectable()
export class RecordService {
getRecords(): Promise<Record[]> {
return Promise.resolve(RECORDS);
}
getRecord(id: string): Promise<Record> {
return this.getRecords()
.then(records => records.find(record => record.id === id));
}
}