如何从对象访问值

时间:2019-06-19 10:27:28

标签: angular typescript

我想将API中的值分配给表单。 我的Api正常工作

console.log(res);的输出

0:
destination: "test@test.com"
domain_id: 2
id: 5
source: "tester0700@test.com"
__proto__: Object

console.log('res是:'+ res.domain_id);

undefined

这就是我获取数据并分配给表单的方式

  this.studentApi.GetStudent(id).subscribe((res => {
      console.log(res);
      this.subjectArray = res;
      console.log('res is: ' + res.domain_id);
      this.studentForm = this.fb.group({
        id: [res.id],
        domain_id: [res.domain_id],
        source: [res.source],
        destination: [res.destination]
      });
    }));

由于未定义,我无法访问输出数据并将其组装成表格。

1 个答案:

答案 0 :(得分:1)

尝试此操作(使用管道):

  import {map} from 'rxjs/operators'

  this.studentApi.GetStudent(id)
  .pipe(map(res => res && res[0]))
  .subscribe((res => {
      console.log(res);
      this.subjectArray = res;
      console.log('res is: ' + res.domain_id);
      this.studentForm = this.fb.group({
        id: [res.id],
        domain_id: [res.domain_id],
        source: [res.source],
        destination: [res.destination]
      });
    }));