微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 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 的?)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。