json - 如何使用 http 模块在 Angular 7 中读取文件 json?
问题描述
在 Angular 7 中读取 JSON 文件
我正在编写一个简单的程序来将一组数据从 json 文件输出到浏览器。
当我尝试读取 json 文件时,出现错误:无法读取未定义的属性“代码”。
app.module.ts :
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
RouterModule.forRoot(appRoutes),
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts :
import { Component } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
countries: any;
constructor(private http : HttpClient) { }
ngOnInit(){
this.http.get("...path to json file...")
.subscribe((data) => this.displaydata(data));
}
displaydata(data) {this.countries = data;}
}
app.component.html :
<table>
<tr >
<th>ID</th >
<th>code</th >
<th>name</th >
<th>population</th >
</tr >
<tr ng-repeat="country in countries">
<td>{{country.code}}</td>
<td>{{country.name}}</td>
<td>{{country.population}}</td>
</tr>
</table>
解决方案
这里:
<tr ng-repeat="country in countries">
ng-repeat
是 AngularJS 语法。你要
<tr *ngFor="let country of countries">
推荐阅读
- javascript - 将 JSON 数组设置为状态,不显示适当的 javascript 对象
- swift - Swift 条带访问 DidCreateToken
- javascript - 如何使 svg 对象可点击
- linux - 无法启动 TeamCity 和构建代理
- mpi - mpi4py 没有放弃 2D 块分解的速度
- node.js - 如何使用不同的请求正文在 nodejs express 中重定向
- python - 将二进制(字节)转换为 str,反之亦然
- python - python - 读取由 php 打印的字符串
- node.js - Gulp 可以用于将图像转换为下一代格式吗?
- sql - 如何编写“有多少不同的客户订购超过 27 次”的 SQL 查询?