我正试图把数据传送到有Angular 的前端.试图从另一个api获取数据,但仍然不适用于我.因此,API正在工作,但数据无法显示在前端.我读了文档,但这无助于解决问题.希望有人能帮助我.

Employee Class:

export class Employee {
id!: number;
firstName!: string;
lastName!: string;
emailId!: string;
}

Employee-list.component.ts

import { Component, OnInit } from '@angular/core';
import { Employee } from '../employee';
import { EmployeeService } from '../employee.service';

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

  employees!: Employee[];

  constructor(private employeeService: EmployeeService) { }

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

  private getEmployees(){
    this.employeeService.getEmployeesList().subscribe(data =>{
      this.employees = data;
    });
  }

}

Employee-list.component.html

<h2> Mitarbeiter Liste</h2>
<table class = "table table-striped">
    <thead>
        <tr>
            <th> Vorname</th>
            <th> Nachname</th>
            <th> Email</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let employee of employees">
            <td>{{ employee.firstName }}</td>
            <td>{{ employee.lastName }}</td>
            <td>{{ employee.emailId }}</td>
        </tr>
    </tbody>
</table>

Employee.service.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Employee } from './employee';
import { HttpClient } from '@angular/common/http';


@Injectable({
  providedIn: 'root'
})
export class EmployeeService {

  private baseUrl = 'http://localhost:8080/api/v1/employees';

  constructor(private httpClient: HttpClient) { }

  getEmployeesList(): Observable<Employee[]>{
    return this.httpClient.get<Employee[]>(this.baseUrl);
  }
}

App.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { EmployeeListComponent } from './employee-list/employee-list.component';

@NgModule({
  declarations: [
    AppComponent,
    EmployeeListComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

The API data:

[{"id":1,"firstName":"Frank","lastName":"wettew","emailId":"test@gmail.de"},{id:2,"firstName":"Herbert","lastName":"Hubert","emailId":"herb@gmail.de"}]

Frontend: enter image description here

推荐答案

我现在解决了这个问题.谢谢你的建议!我需要添加到我的控制器@CrossOrigin(origins="localhost:4200")

Angular相关问答推荐

升级到angular 17并转换为独立的加载器拦截器后,

使Angular 效果只执行一次

如何在Angular中执行一个操作条件为多个可观测值?

如果Angular 模块是独立的,为什么我不需要在App模块中使用RouterModule?

无法添加@angular/pwa

如果过滤器在rxjs流中触发,则返回空数组

带迭代的Angular 内容投影

material 对话框中没有合适的注塑

Angular :为什么我得到了一个可观察到的共鸣

Angular 懒惰地加载一个服务

Angular 16 Auth Guard在具有0的服务时给出Null Injector错误;httpclient;被注入

NgRx Effects 抛出类型错误,我不知道发生了什么

在一个组件中创建多个表单

如何处理解析器中的错误

URL 清理导致嵌入式 YouTube 视频的刷新

Angular为 4 的可扩展表格行,带有Angularmaterial

Angular将 Select 值转换为 int

如何在 Angular 应用程序中通过路由更改页面标题?

angular2 测试,我如何模拟子组件

Angular2:将数组转换为 Observable