首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将从服务器接收的数据发布到客户端网站

如何将从服务器接收的数据发布到客户端网站
EN

Stack Overflow用户
提问于 2018-12-13 11:58:49
回答 1查看 56关注 0票数 0

我从服务器接收以下数据到客户端,并存储在res变量中:

代码语言:javascript
复制
0: {date: "2014-12-02T00:00:00", close: 106.9064, buySell: "Sell"}
1: {date: "2014-12-03T00:00:00", close: 108.1188, buySell: "Sell"}
2: {date: "2014-12-04T00:00:00", close: 107.7084, buySell: "Sell"}

这是将上述内容打印到控制台窗口的角度代码:

代码语言:javascript
复制
  getBuySellData(){
     this.http.get(this.stockURL).subscribe(res => {
     console.log(res)
})

我想将这些数据打印到客户端屏幕上。这就是我现在正在尝试的:

代码语言:javascript
复制
 <mat-list >
      {{stock}}
 </mat-list>

这将在客户端网站上打印:

代码语言:javascript
复制
[object Object]

以下是这些组件:

代码语言:javascript
复制
export class ApiService {

constructor(private http: HttpClient) { }

stock: StockComponent
list: any[]
stockURL = 'https://localhost:44310/api/stock'

/** POST: */
postStock (stock) {
  this.http.post(this.stockURL, stock).subscribe(res => {
    console.log(res)
  })
}

getBuySellData() {
  this.http.get(this.stockURL).subscribe(res => {
    this.stock = res;
  })
}

股票构成部分

代码语言:javascript
复制
@Component({
selector: 'stocks',
templateUrl: './stocks.component.html' 
})
export class StocksComponent {
  stock = {}
constructor(private api: ApiService){

}

ngOnInit() {
  this.api.getBuySellData()
}

post(stock){
     this.api.postStock(stock)
}

股票成分

代码语言:javascript
复制
@Component({
selector: 'stock',
templateUrl: './stock.component.html' 
})
export class StockComponent {
 stock = {}
  constructor(private api: ApiService){
  }

ngOnInit() {
  this.api.getBuySellData()
}

post(stock){

    this.api.postStock(stock)
}

股票HTML

代码语言:javascript
复制
                <mat-list>
                    {{stock.date}}
                  </mat-list>
            <mat-list>
                <mat-list-item *ngFor="let item of stock">
                    {{item.date | date}}
                </mat-list-item>
             </mat-list>
EN

回答 1

Stack Overflow用户

发布于 2018-12-13 12:15:12

尝试这样做:首先,通过将结果分配给成员列表来编辑TypeScript逻辑。

代码语言:javascript
复制
list: any[];
getBuySellData(){
     this.http.get(this.stockURL).subscribe(res => {
     this.list = res;
})

然后,在模板中,确保使用异步管道,以便通知角,由于使用了可观察到的原因,您试图显示的数据是异步的。

实现可能略有不同,我还没有对其进行测试,但是要记住的核心是,删除这个丑陋的对象是使用异步管道。

代码语言:javascript
复制
<mat-list>
   <mat-list-item *ngFor="let item of list">
    {{item | async}}
  </mat-list-item>
</mat-list>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53761377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档