【发布时间】:2019-08-11 14:11:22
【问题描述】:
我正在尝试根据相应表单的 id 获取文档 "TypeError: Cannot read property 'id' of undefined" 响应中出现错误: 按钮点击的html如下:
<div class="form-group row">
<div class="col-md-12" style="text-align: center;">
<button (click)="onNext(i)" type="submit" class="btn btn-primary shadow" style="float: right;">NEXT</button>
</div>
</div>
而我各自的.ts文件如下图:
import { Component, OnInit } from '@angular/core';
import { UsersService } from 'src/app/services/users.service';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-admin-higherstudies-form',
templateUrl: './admin-higherstudies-form.component.html',
styleUrls: ['./admin-higherstudies-form.component.scss'],
providers: [UsersService]
})
export class AdminHigherstudiesFormComponent implements OnInit {
formData: any[];
constructor(private userService: UsersService, private router: Router, private activatedRoute: ActivatedRoute) { }
ngOnInit() {
let formId = this.activatedRoute.snapshot.paramMap.get('formId');
this.userService.gethigherStudiesFormList(formId).subscribe(
response => {
this.formData = response;
console.log(response);
}
);
}
logout() {
return this.userService.logout();
}
Back() {
this.router.navigate(['/admin-higher-studies']);
}
onNext(i) {
let path = '/admin-higherstudies-documents' + this.formData[i].id;
this.router.navigate([path]);
}
}
我的服务代码如下:
gethigherStudiesphdDocumentsList(formid): Observable<any> {
const httpoptions = {
headers: new HttpHeaders({ 'Authorization': 'token ' + localStorage.getItem('token') })
};
return this.http.post('http://127.0.0.1:8000/api/higher-studies-phd-documents/' + formid + '/', httpoptions);
}
当我尝试在代码中提到的按钮 div 类中使用 ngFor 时:
<div class="form-group row">
<div class="col-md-12" *ngFor="let data of formData index as i" style="text-align: center;">
<button (click)="onNext(i)" type="submit" class="btn btn-primary shadow" style="float: right;">NEXT</button>
</div>
</div>
我收到的错误是 找不到类型为“名称”的不同支持对象“[对象对象]”。 NgFor 只支持绑定到数组等 Iterables。
【问题讨论】:
标签: angular