如何在 Angular 中查看数据中的字段订阅

问题描述

我想根据IdEmployee选择我想放入tasks数组的Objects 我试图显示数据的内容(其中有字段“IdEmployee”),但是当我显示它时,我可以看到:data.IdEmployee = undefined

在 html 文件中,我可以从我的数据库中看到整个数组

@Component({
  selector: 'app-display-tasks',templateUrl: './display-tasks.component.html',styleUrls: ['./display-tasks.component.css']
})
export class displayTasksComponent implements OnInit {

  constructor(private http:HttpClient) { }
  tasks:any=[]

  ngOnInit(): void {
    this.refreshList();
  }

  refreshList(){
    this.http.get<any>(environment.API_URL+'task')
    .subscribe(data=>{
      this.tasks=data;
      alert("data.IdEmployee = "+data.IdEmployee)
    })
  }
}

解决方法

您将对象数组作为数据返回。

试试:

alert("data.IdEmployee = " + tasks[0].IdEmployee)

通过这种方式,您将获取数组中第一个对象的 IdEmployee。

如果您总是只返回一个对象,您可能需要考虑将后端更改为只返回一个对象而不是一组对象。

(我怀疑你想遍历一个数组并提醒所有 IdEmployee 的?)