Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular web api对角度类映射的响应_Angular_Asp.net Web Api - Fatal编程技术网

Angular web api对角度类映射的响应

Angular web api对角度类映射的响应,angular,asp.net-web-api,Angular,Asp.net Web Api,我有返回chapterid和chaptername的web api操作方法。我想将其映射到angular类,并使用默认值为false的额外字段“Edit” export class Chapter { chapterid: number; chaptername: string; Edit: boolean = false; } public Chapters: any; this.http.get<Chapter>(this.baseUrl + 'api/Chap

我有返回chapterid和chaptername的web api操作方法。我想将其映射到angular类,并使用默认值为false的额外字段“Edit”

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));
导出类章节{
第四章:数字;
章节名称:字符串;
编辑:布尔=假;
}
公开章节:任何;
this.http.get(this.baseUrl+'api/Chapters').subscribe((响应)=>{this.Chapters=response;console.log(this.Chapters);},error=>console.log(error));

我没有得到额外的字段“编辑”。

看起来像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));
this.http.get(this.baseUrl+'api/Chapters')
.烟斗(
地图(章节=>{
return chapters.map(c=>{return{…c,Edit:false});
}),                     
)
.subscribe((response)=>{this.Chapters=response;console.log(this.Chapters);},error=>console.log(error));

一旦传输应用程序,此响应的键入基本上消失。他们只是为你,开发人员,使你的生活更容易在开发过程中,他们不会自动修改响应有额外的领域。您需要手动实例化和设置这些字段。你可以在你的类上设置一个构造函数,并用它来填充额外的字段,但否则它将无法按你的意愿工作。导出类章节{chapterId:number;chapterName:string;Edit:boolean;构造函数(chapterId:number,chapterName:string){this.chapterId=chapterId;this.chapterName=chapterName;this.Edit=false;}但是它不起作用。我做得对吗?它起作用了。解决方案中缺少一个返回。this.http.get(this.baseUrl+'api/Chapters').pipe(map(Chapters=>{return Chapters.map(c=>{return{…c,Edit:false}}})).subscribe(val=>console.log(val));