[英]Angular 4 : FormArray gives “ERROR TypeError: Cannot read property 'id' of undefined”
[英]ERROR TypeError: Cannot read property 'id' of undefined (Angular)
大家好,我一直收到此錯誤,我剛開始學習Angular時不確定如何解決此錯誤。 有人可以幫我弄這個嗎?
component.ts
export class AdminUser implements OnInit {
Form: FormGroup;
currentUser: User;
user: Array<any> = [];
admin: Array<any> = [];
searchText: string;
id:string;
constructor(private fb: FormBuilder,
private individualSrv: IndividualService,
private userSrv: UserService,
private router: Router
) {
let res = individualSrv.getAll();
res.then(result => {
result.subscribe(_user => {
this.user = _user;
});
});
}
navigate(path: string) {
this.router.navigate(["/admin/" + path],{
queryParams: {id: this.currentUser.id}
});
component.html
<ng-container *ngFor="let content of user">
<tr>
<td>{{content.fullname}}</td>
<td>{{content.createddate | date}}</td>
<td><a (click)="navigate('userSetting')"><i class="fa fa-cog"></i> Setting</a></td>
</tr>
HTML代碼:
<ng-container *ngFor="let content of user">
<tr>
<td>{{content.fullname}}</td>
<td>{{content.createddate | date}}</td>
<td><a (click)="navigate('userSetting')"> // This is wrong
//<td><a (click)="navigate(content)"> // Use this instead
<i class="fa fa-cog"></i> Setting</a></td>
</tr>
</ng-container>
您的導航可能是這樣的:
navigate(user) {
this.router.navigate(["/admin/" + user.path],{
queryParams: {id: this.user.id}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.