在angular 10中映射和对象到objectDto

在angular 10中映射和对象到objectDto,angular,rest,dictionary,model,webapi,Angular,Rest,Dictionary,Model,Webapi,我正在努力将来自webapi的对象映射到Angular 10中的objectDto。我想在Dto中映射一个新字段,etatIcon 怎么做 这是来自WebApi的我的对象 { "idAlerte": 1, "codeType": "ACQ", "codeEtat": "YZ" } 我的模特是角型的 export interface ISosAlerte {

我正在努力将来自webapi的对象映射到Angular 10中的objectDto。我想在Dto中映射一个新字段,etatIcon

怎么做

这是来自WebApi的我的对象

{
    "idAlerte": 1,
    "codeType": "ACQ",
    "codeEtat": "YZ"   
}
我的模特是角型的

export interface ISosAlerte {
    idAlerte: number;
    codeType: string;
    codeEtat: string;
    etatIcon: string;
}

export class SosAlerte implements ISosAlerte{ 
    idAlerte: number;
    codeType: string;
    codeEtat: string;
    etatIcon: string;

    constructor(
         idAlerte: number = null,
         codeType: string = '',
         codeEtat: string = '',
         etatIcon: string = ''
    )
    {
        this.idAlerte=idAlerte;
        this.codeType=codeType;
        this.codeEtat=codeEtat;

        if(this.codeType ==="ACQ")
            this.etatIcon = './../../../assets/images/red.png';
        else
            this.etatIcon = './../../../assets/images/green.png';
    }
}
我将此值加载到我的service.ts中

  public alertEnCours: SosAlerte[] = [];

  public load(): Observable<boolean> {
    this.isBusy = true;
    if (this.isLoaded) {
                        return new Observable((observer) => {
                          this.isBusy = false;
                          observer.next(true);
                          observer.complete();
                        });
    } else {
            return this.httpClient.get(this.ENDPOINT_URL,this.httpOptions)
            .pipe(  
                    catchError(this.handleError),          
                    map((data: SosAlerte[]) => { 
                      this.isBusy = false;                  
                      this.alertEnCours = data;   <== Here I never have the etatIcon mapped
                      this.isLoaded = true;                    
                      return true;
                    }),
                  );
    }
  }
public-alertEnCours:SosAlerte[]=[];
公共负载():可观察{
this.isBusy=true;
如果(此.已加载){
返回新的可观察对象((观察者)=>{
this.isBusy=false;
观察者:下一个(正确);
observer.complete();
});
}否则{
返回this.httpClient.get(this.ENDPOINT\u URL,this.httpOptions)
.管道(
catchError(此为handleError),
地图((数据:SosAlerte[])=>{
this.isBusy=false;

this.alertEnCours=data;据我所知,您从未调用过
SosAlerte
的构造函数,那么映射应该如何发生?不应该是
SosAlerte[]
的返回类型,而应该是
map()
的类型,而应该是传入数据的类型?我想您应该在
map()中尝试类似的方法
callback:
this.alertEnCours=data.map(item=>new-SosAlerte(item))
谢谢你的Ibsn。它现在可以工作了。我还在学习。