html - 无法将 url 读取为 JSON 数据
问题描述
我有这个 JSON 数据,我正在使用 Angular 将其输出到 html 文件。我使用一个服务来获取 JSON 数据并使用一个组件来创建模板。使用模板后,我只得到 id 值和 name 值,而不是 url 值。
JSON:
{
"A":1,
"B":[
{
"id":1,
"name":"One",
"url":"http://myapp/rootpath/first"
},
{
"id":2,
"name":"two",
"url":"http://myapp/rootpath/second"
}
]
}
我将 JSON 数据带到组件中的 postArray 变量并将其传递给 html 文件。
search.service.ts:
import { Injectable } from '@angular/core';
import {Http,Response} from "@angular/http";
import { Observable } from "rxjs";
import "rxjs/Rx";
import {JsoncallItem} from "./jsoncall-item";
@Injectable({
providedIn: 'root'
})
export class ApiService {
private postsURL ="http://myapp/items";
constructor(private http: Http ) {}
getPosts(): Observable<JsoncallItem[]>{
return this.http
.get(this.postsURL)
.map((response: Response)=> {
return <JsoncallItem[]>response.json();
})
.catch(this.handleError);
}
private handleError(error: Response) {
return Observable.throw(error.statusText);
}
}
search.component.ts
import { Component, OnInit } from '@angular/core';
import { ApiService } from "./../search.service";
import { JsoncallItem } from "./../jsoncall-item";
import { error } from 'util';
import { SearchtermPipe } from './../searchterm.pipe';
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
title = 'app';
_postsArray: JsoncallItem[];
constructor(private apiSerivce: ApiService){}
getPosts(): void {
this.apiSerivce.getPosts().
subscribe(
resultArray => this._postsArray = resultArray
)
//error => console.log("Error :: " + error ))
}
ngOnInit(): void{
this.getPosts();
}
}
search.component.html:
<div class="container">
<ul>
<li *ngFor="let post of _postsArray | searchterm: value">
<a href="" id="allitem">{{post.id}}, {{post.name}}, {{post.url}}</a>
<hr>
</li>
</ul>
</div>
输出显示每个数据的 id 和名称,但不显示 url。这里可能有什么问题?
解决方案
推荐阅读
- reactjs - 即使道具保持不变也触发useEffect?
- dart - 如何在 Dart Aqueduct 服务器中获取请求标头
- sql-server - 具有固定参数的 SQL Server 用户定义函数
- php - PHP mysqli 多个查询 bind_param 崩溃
- regex - 匹配运算符左侧的数组
- javascript - 如何使用 javascript 用动态内容覆盖子节点值
- java - 如何自下而上在网格中添加对象?
- javascript - 我们如何使用 mysqli 将包含来自 xml http 请求的文本的数组发送到 PHP?
- dask - 创建后是否可以将工作人员资源分配给分布式工作人员?
- reactjs - 为什么只有从循环生成的元素才能获得“关键”属性?