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);
});
}
}
添加回答
举报