首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角7订阅填充模型阵列

角7订阅填充模型阵列
EN

Stack Overflow用户
提问于 2019-07-30 02:04:24
回答 1查看 433关注 0票数 0

我使用的是角7,我想用订阅返回填充我的数组模型。

当我这么做时:

代码语言:javascript
复制
  rsvp: RSVP[];

  constructor(private rsvpService: EventoRsvpService) { }

  ngOnInit() {
     this.rsvpService.obterListaRSVP().subscribe(
       rsvp => {
         this.rsvp = rsvp
         console.log(this.rsvp)
       }
     )
  }

我在控制台上有:

当我在*ngFor上使用它时,例如,total_confirmados可以与{{rsvp.total_confirmados}一起使用。但我的RSVP模型中没有total_confirmados。看:

代码语言:javascript
复制
export class RSVP{
    nome: string
    email: string
    totalConfirmados: number
}

这是正确的吗?还是应该在订阅返回中工作,以在适当的字段中填充它们的值?我更喜欢使用totalConfirmados ( RSVP模型)而不是total_confirmados (JSON返回)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-30 02:12:50

您需要转换为适当的格式。

代码语言:javascript
复制
ngOnInit() {
 this.rsvpService.obterListaRSVP().subscribe(
   rsvp => {
     this.rsvp = rsvp.map(r => ({
        nome: r.nome, 
        email: r.email,
        totalConfirmados: r.total_confirmados      
      }));
     console.log(this.rsvp)
   }
 )
}

但是,将代码重构为更好的格式(而不是订阅)是这样的:

代码语言:javascript
复制
rsvp$: Observable<RSVP[]>;

constructor(private rsvpService: EventoRsvpService) { }

ngOnInit() {
 this.rsvp$ = this.rsvpService.obterListaRSVP().pipe(
   map( rsvp => rsvp.map(r => ({
        nome: r.nome, 
        email: r.email,
        totalConfirmados: r.total_confirmados      
      }))
   ),       
 )
}

^上面的代码,第一个映射是RxJ,第二个映射是数组。

在html中

代码语言:javascript
复制
*ngFor="let rsvp of rsvp$|async" 
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57263411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档