我有Web API操作方法,返回的ChapterID和ChapterName。我想使用默认值为false
的额外字段“ Edit”将其映射到angular类。export class Chapter {
chapterid: number;
chaptername: string;
Edit: boolean = false;
}
public Chapters: any;
this.http.get<Chapter>(this.baseUrl + 'api/Chapters').subscribe((response) => { this.Chapters = response; console.log(this.Chapters); }, error => console.log(error));
我没有得到额外的字段“编辑”。
答案 0 :(得分:1)
类似于API api/Chapters
返回具有chapterid
和chaptername
的对象数组。如果是这样,则像这样更新您的代码:
this.http.get<Chapter[]>(this.baseUrl + 'api/Chapters')
.pipe(
map(chapters => {
return chapters.map(c => {return {...c, Edit: false}});
}),
)
.subscribe((response) => { this.Chapters = response; console.log(this.Chapters); }, error => console.log(error));