我正在尝试使用Yahoo Finance查找名称和股票价格。 尝试使用ngFor遍历结果。我无法在html文件中显示结果。结果已成功显示在控制台上。我认为服务文件工作正常。
Github:https://github.com/Manasipotade/stock-app 用于显示结果的文件
.ts文件
import { Component, OnInit, Input } from '@angular/core';
import { StockService } from 'app/stock.service';
import {Observable, BehaviorSubject } from 'rxjs';
@Component({
selector: 'stock-app',
templateUrl: './stock-app.component.html',
styleUrls: ['./stock-app.component.scss']
})
export class StockAppComponent implements OnInit {
stocks$: Observable<any>;
stockSymbol: string;
constructor(
private _stockService: StockService
) { }
ngOnInit() {
this.stocks$ = this._stockService.stocks$;
}
}
html文件
<div class="container-fluid">
<div class="row">
<div class="col-4">
<stock-input></stock-input>
</div>
<div class="col-8">
<table class="table table-stripped">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let stock of stocks$ | async">
<td>
{{ stock | json }}
</td>
</tr>
</tbody>
</table>
</div>
的StockService
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import {Observable, BehaviorSubject } from 'rxjs';
@Injectable()
export class StockService {
private _stocks$: BehaviorSubject<any> = new BehaviorSubject<any>([]);
public readonly stocks$ = this._stocks$.asObservable();
constructor(
private _http: Http
) { }
getStock(stockSymbol:string):Observable<any []>{
let searchQuery ='select * from yahoo.finance.quotes where symbol={$stockSymbol} ';
const stocklookupUrl:string='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20%3D%20%27'+stockSymbol+'%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=';
let headers = new Headers();
headers.append('Content-Type','application/json');
return this._http.get(stocklookupUrl).map((res:Response) =>{
this._stocks$.next([...this._stocks$.value,res.json]);
console.log(this._stocks$.value);
return res.json();
})
}
}
答案 0 :(得分:0)
由于您想使用for循环显示名称和价格,我假设您的服务响应应该是对象数组,其中每个对象代表一个股票,并且名称和价格作为其键。如果是这种情况,您的代码是不正确的。它应该是:
<tbody>
<tr *ngFor="let stock of stocks$ | async">
<td>
{{ stock.name }}
</td>
<td>
{{ stock.price }}
</td>
</tr>
</tbody>
答案 1 :(得分:0)
由于JS是异步的,你无法在HTML dom中请求和分配http响应值,因为在收到响应之前,html不会保持呈现。 我建议你可以从主组件调用http请求并在服务变量中存储响应。并从您的子组件访问该特定变量。 当你需要更新服务变量时,你可以通过触发请求来刷新。
import { Component, OnInit, Input } from '@angular/core';
import { StockService } from 'app/stock.service';
import {Observable, BehaviorSubject } from 'rxjs';
@Component({
selector: 'stock-app',
templateUrl: './stock-app.component.html',
styleUrls: ['./stock-app.component.scss']
})
export class StockAppComponent implements OnInit {
stocks$: Observable<any>;
stockSymbol: string;
constructor(
private _stockService: StockService
) { }
ngOnInit() {
this.stocks$ = this._stockService.stockObj; // get updated response from service object
}
}
和服务
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import {Observable, BehaviorSubject } from 'rxjs';
@Injectable()
export class StockService {
stockObj : any;
private _stocks$: BehaviorSubject<any> = new BehaviorSubject<any>([]);
public readonly stocks$ = this._stocks$.asObservable();
constructor(
private _http: Http
) { }
getStock(stockSymbol:string):Observable<any []>{
let searchQuery ='select * from yahoo.finance.quotes where symbol={$stockSymbol} ';
const stocklookupUrl:string='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20%3D%20%27'+stockSymbol+'%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=';
let headers = new Headers();
headers.append('Content-Type','application/json');
return this._http.get(stocklookupUrl).map((res:Response) =>{
this._stocks$.next([...this._stocks$.value,res.json]);
console.log(this._stocks$.value);
this.stockObj = this._stocks$.value; // Assign response to variable stockObj
return res.json();
})
}
答案 2 :(得分:0)
<强> StockAppComponent 强>
import { Component, OnInit, Input } from '@angular/core';
import { StockService } from 'app/stock.service';
import { Observable, BehaviorSubject } from 'rxjs';
@Component({
selector: 'stock-app',
templateUrl: './stock-app.component.html',
styleUrls: ['./stock-app.component.scss']
})
export class StockAppComponent implements OnInit {
stocks$: Observable<any>;
stockSymbol: string;
constructor(
private _stockService: StockService
) { }
ngOnInit() {
this._stockService.getStock(this.stockSymbol).subscribe((response: any) => {
this.stocks$ = response;
}, (error: any) => {
console.log(JSON.stringify(error));
});
}
}