Angular HTTPClient 不显示来自我的 WebApi 的响应并将变量视为未定义

Pan*_*hal 1 html c# asp.net angular

我正在学习AngularASP.NET,所以我想制作简单的 WebApi,但在邮递员中一切正常,唯一的问题是我的 Angular 应用程序,我不知道在哪里,所以我在这里寻求建议。

这是我的 Angular .ts 脚本,负责从本地 api 获取数据:

import { Component, OnInit } from "@angular/core";
import { HttpClientModule } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class ConfigService {
  constructor(private http: HttpClient) { }
}

@Component({

  selector: "app-user",
  templateUrl: "./user.component.html",
  styleUrls: ["./user.component.css"]
})
export class UserComponent implements OnInit {
  values: any;

  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.getValues();
    console.log();
  }

  getValues() {
    this.http.get('http://localhost:5000/api/DataCTX').subscribe(response => {
       this.values = response;
    }, error => {
       console.log(error);
      });
    }

    // przypisanie warto?ci do values, wypisywanie b??du

}
Run Code Online (Sandbox Code Playgroud)

正如我在调试日志中看到的那样,它正在正常工作,并且没有出现错误。在我的 WebApi (localhost:5000) 中,我从数据库中获取了一些用户数据:

这是模型:

namespace WebAPI_1.Models
{
    public class Users
    {
        public int id { get; set; }
        public string Imie { get; set; }
        public string Nazwisko { get; set; }
        public string Login { get; set; }
        public string Haslo { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想从我的应用程序中获取其中一些值,并使用以下标记将其写在 html 中:

<p *ngFor="let a of values">
  {{values.id}} - {{values.Imie}}
</p>
Run Code Online (Sandbox Code Playgroud)

但我唯一得到的是:

结果在浏览器

所以,如果你想帮助我,我会很高兴,问候:D

Mus*_*san 5

您正在访问id and imie整个列表而不是aforeach 中的对象,所以试试这个:

<p *ngFor="let a of values">
  {{a.id}} - {{a.imie}}
</p>
Run Code Online (Sandbox Code Playgroud)

  • 我希望今天你会遇到一些好人,很棒的人&lt;3 (2认同)