3 回答

TA貢獻1828條經驗 獲得超3個贊
代碼中的所有內容都沒有問題,唯一的問題是該屬性jobTitle是Object
您有 3 個可用選項(或更多)
選項1
從后端以字符串形式返回值;這意味著我們可以將您的界面修改為
export interface IEmployee {
userId: any;
firstName: string;
jobTitle: string;
lastName: String;
}
選項2
更改接口以反映Object來自后端的
export interface IEmployee {
userId: any;
firstName: string;
jobTitle: { jobTitleName: string; jobTitleId: number };
lastName: String;
}
現在您可以在您的 html 中使用它,如下所示。這種方法已由@shehanpathirathna 闡述
<td>{{ p.jobTitle.jobTitleName }}</td>
選項3
用于map生成具有所需結構的新對象
import { map } from 'rxjs/operators';
export class VisibilityComponent implements OnInit {
pmDetails1: IEmployee[];
constructor(private userService: UserService) {}
ngOnInit() {
this.userService.getAllUsers().pipe(
map(employeea => employees.map(
(employee) => ({...employee, jobTitle: jobTitle.jobTitleName })
))
).subscribe((pmDetails1: IEmployee[]) => {
this.pmDetails1 = pmDetails1;
console.log(pmDetails1);
console.log(pmDetails1[0].jobTitle);
});
}
}
對于這種特定情況,此選項可能有點過大,但如果對象變得更復雜,則可能非常有幫助

TA貢獻1824條經驗 獲得超8個贊
嘗試這個 :
export class VisibilityComponent implements OnInit {
pmDetails1: IEmployee[];
constructor(private userService: UserService) {}
ngOnInit() {
this.userService.getAllUsers().subscribe(data => {
this.pmDetails1 = data;
console.log(pmDetails1);
console.log(pmDetails1[0].jobTitle);
});
}
}
添加回答
舉報