如何显示从Angular中的后端API收到的数据
我目前正在尝试显示员工将其记录到他的帐户后的数据,我可以通过他的用户名从后端获取员工的数据,我可以安装它记录在后端,但是当我将数据分配给类型的另一个变量时,我控制了该变量,结果我的登录不确定。
我的服务文件:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { Employee } from '../Employee';
@Injectable({
providedIn: 'root'
})
export class EmployeeService {
private apiServerUrl = environment.apiBaseUrl;
constructor(private http: HttpClient) { }
getEmployee(username: String): Observable<any> {
return this.http.get<any>(`${this.apiServerUrl}/getbyuser/${username}`);
}
}
lightee.component.ts文件
import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { EmployeeService } from '../services/employee.service';
@Component({
selector: 'app-employee',
templateUrl: './employee.component.html',
styleUrls: ['./employee.component.css']
})
export class EmployeeComponent implements OnInit {
constructor(public auth: AuthService,private api: EmployeeService) { }
employeeObj : any;
ngOnInit(): void {
let username = localStorage.getItem('username');
if(username != null) {
this.getEmployee(username);
console.log(this.employeeObj); // it shows undefined
}
}
getEmployee(username: String) {
this.api.getEmployee(username).subscribe(
(data) => {
this.employeeObj = data;
console.log(this.employeeObj); // it shows the informations i received from my backend
}, (error: HttpErrorResponse) => {
console.log(error);
}
)
}
}
结果:
Im currently trying to show the data of an employee after he loggin to his account, i can get the data of the employee from the backend by his username and i can console logged it , but when i assign the data to another variable of type any i console log that variable and i got undefined as a result.
my service file :
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { Employee } from '../Employee';
@Injectable({
providedIn: 'root'
})
export class EmployeeService {
private apiServerUrl = environment.apiBaseUrl;
constructor(private http: HttpClient) { }
getEmployee(username: String): Observable<any> {
return this.http.get<any>(`${this.apiServerUrl}/getbyuser/${username}`);
}
}
employee.component.ts file
import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { EmployeeService } from '../services/employee.service';
@Component({
selector: 'app-employee',
templateUrl: './employee.component.html',
styleUrls: ['./employee.component.css']
})
export class EmployeeComponent implements OnInit {
constructor(public auth: AuthService,private api: EmployeeService) { }
employeeObj : any;
ngOnInit(): void {
let username = localStorage.getItem('username');
if(username != null) {
this.getEmployee(username);
console.log(this.employeeObj); // it shows undefined
}
}
getEmployee(username: String) {
this.api.getEmployee(username).subscribe(
(data) => {
this.employeeObj = data;
console.log(this.employeeObj); // it shows the informations i received from my backend
}, (error: HttpErrorResponse) => {
console.log(error);
}
)
}
}
the result :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于代码的异步执行,该执行导致变量在实际分配该值之前被记录。
这个问题可能有多种解决方案,但这取决于您实际想对代码做什么。
如果您仅在html中使用
employeeeobj
的数据,则可以在html中使用它,那么您只需检查值是否未定义,并且当数据完成填充时,它将自动更新。您还可以在
getemployeee
函数的内部使用雇员
进行任何操作。否则,您可以使用 async/等待
This is due to the asynchronous execution of the code which causes the variable to be logged before the value has actually been assigned.
There could be multiple solutions to this problem, but it depends on what you actually want to do with your code.
If you are only using data from
employeeObj
in the HTML after pulling it, then you could simply just check if the value is undefined and it will automatically updated when the data finishes populating.You also could just do anything you need to do with your
employeeObj
inside of yourgetEmployee
function.Otherwise you can make use of promises and async/await