Json Ionic3编码-尝试检索当天的报价
Json Ionic3编码-尝试检索当天的报价,json,ionic-framework,ionic2,ionic3,Json,Ionic Framework,Ionic2,Ionic3,provider.ts文件中的代码: getQuotes() : Observable<any> { return this.http.get(""); } .html代码尝试了此操作,但无效: <h2> {{ quote}} </h2> <h3> {{ author }}</h3> </ion-item> </ion-list> .html文件代码*: <ion-list>
provider.ts
文件中的代码:
getQuotes() : Observable<any> {
return this.http.get("");
}
.html
代码尝试了此操作,但无效:
<h2> {{ quote}} </h2>
<h3> {{ author }}</h3>
</ion-item> </ion-list>
.html
文件代码*:
<ion-list>
<ion-item *ngFor = "let q of quote">
<h2> {{ q.quote}} </h2>
<h3> {{q.author }}</h3>
</ion-item>
</ion-list>
{{q.quote}
{{q.author}}
在provider.ts
中,您应该执行以下操作:
import 'rxjs/add/operator/map';
getQuotes(){
return this.http.get("").map(res => res.json());
}
然后在home.ts中
ionViewDidLoad()
{
this.qp.getQuotes().subscribe(data => {
this.quote = data.contents.quotes[0].quote;
this.author = data.contents.quotes[0].author;
// This is assuming the quotes array always has only one quote
});
}
编辑
如前所述,您还需要删除home.html
中的for循环:
<ion-list>
<ion-item>
<h2> {{ quote }} </h2>
<h3> {{ author }}</h3>
</ion-item>
</ion-list>
{{quote}}
{{作者}
>ionViewDidLoad(){>this.qp.getQuotes().subscribe(data=>{>this.quote=data.contents.quotes.quote;>this.author=data.contents.quotes.author;>});他也需要从html中删除for循环。@san您需要将导入'rxjs/add/operator/map';
添加到您的服务提供商,这样它就可以编辑问题以反映您需要做的事情do@san问题是你的地址错了,在我的回答中你可以看到我使用了quote.rest/qod.json。错误是你使用了quote.rest/qod/json报价的剩余部分/qod。json@san我想你需要更多的代表来开一个聊天室,我也是。你需要什么帮助?
ionViewDidLoad()
{
this.qp.getQuotes().subscribe(data => {
this.quote = data.contents.quotes[0].quote;
this.author = data.contents.quotes[0].author;
// This is assuming the quotes array always has only one quote
});
}
<ion-list>
<ion-item>
<h2> {{ quote }} </h2>
<h3> {{ author }}</h3>
</ion-item>
</ion-list>