【发布时间】:2018-08-02 21:32:39
【问题描述】:
我是一个新的 Angular 开发人员,但我不知道我错在哪里。我找到了很多来源,但我没有调整我的代码。我的代码不起作用或无法从网络客户端获取您能帮我解决这个问题吗?
我的 JSON 数据示例:
{ “id”:“123aCaA-9852”, "name": "一家银行", “地址”:“地址”, "contactName": "示例姓名", "contactSurname": "示例", "联系电话": "12312312312", “状态”:假, “城市”: { “身份证”:40, “名称”:“土耳其”, “代码”:34 }, 我的示例 ts:
export class Kafein {
name:string;
address:string;
}
我的例子 kafein.component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Kafein } from './kafein';
import { Observable } from 'rxjs/internal/Observable';
@Component({
selector: 'app-kafein',
templateUrl: './kafein.component.html',
styleUrls: ['./kafein.component.css']
})
export class KafeinComponent implements OnInit {
title = "Kafein";
//kafein:Observable<Kafein[]>;
kafein:Kafein[];
constructor(private http: HttpClient) { }
ngOnInit() {
this.getProducts();
}
getProducts(){
//this.kafein = this.http.get<Kafein[]>('http:example');
this.http.get<Kafein[]>('http:example').subscribe(res=>{this.kafein=res});
}
}
我的示例组件.html
<tr *ngFor='let kafein of Kafein'>
<th scope="row">1</th>
<td>{{kafein.name}}</td>
<td>{{kafein.adress}}</td>
</tr>
【问题讨论】:
-
控制台有什么错误?
-
在我的屏幕上返回空数据
-
@EmreSert 你试过这个吗:
this.kafein=res as Kafein[] -
我解决了这个问题。非常感谢。