如何在Angular中查看data (subscribe)中的字段



我要根据ideemployee选择要放入任务数组的对象我试图显示数据的内容(其中有字段&;ideemployee&;),但是当我显示它时,我可以看到:数据。ideemployee = 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)

这样你就获取了数组中第一个对象的ideemployee。

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

(我怀疑你想迭代一个数组并提醒所有的ideemployee的?)

最新更新