我学习角度和我正在创建一个加密货币交换应用程序。我创建了一个服务和一个接口来从api获取数据。现在我可以将其绑定到dom,但我也希望在component.ts中使用此数据,以便可以编写例如:
bid2=bid*2;
然后将该变量绑定到dom,如下所示:{{bid2}
谢谢你的帮助。这是我的代码:
组件.ts

import { Component, OnInit } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { BittrexService } from '../../bittrex/bittrex.service';
import {Observable} from "rxjs";
import { MarketListObject } from '../datosmoneda';
import { MarketPrices } from '../datosmoneda';

@Component({
  selector: 'app-comprarzec',
  templateUrl: './comprarzec.component.html',
  styleUrls: ['./comprarzec.component.scss']
})
export class ComprarzecComponent implements OnInit {

  private prices = [];


  constructor(private bittrexService: BittrexService) {
    this.bittrexService = bittrexService;
  }

ngOnInit(){
  this.bittrexService.getPrices()
  .subscribe(
    data => this.prices = data.result
  );
}
 }

服务.ts
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {Observable} from "rxjs";
import 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import { MarketViewModel } from '../comprarmonedas/datosmoneda'


@Injectable()
export class BittrexService {

  constructor(private http: Http, private marketModel : MarketViewModel) { }

  public getPrices() :Observable<MarketViewModel> {
    return this.http.get('https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-zec')
    .map((response: Response) => response.json());
  }

}

接口(datosmoneda.ts);
export class MarketViewModel {
  public success : boolean;
  public message : string;
  public result : MarketListObject[];
}

export class MarketListObject {
    public MarketName : string;
    public High : number;
    public Low : number;
    public Volume : number;
    public Last : number;
    public BaseVolume : number;
    public TimeStamp : number;
    public Bid : number;
    public Ask : number;
    public OpenBuyOrders : number;
    public OpenSellOrders : number;
    public PrevDay : number;
    public Created : number;

}

再次感谢你的帮助!

最佳答案

bid2=bid*2;
然后将该变量绑定到dom,如下所示:{{bid2}
首先值得注意的是,要使{{ Bid2 }}工作,Bid2需要是ComprarzecComponent上的一个属性,但是BidMarketListObject上的一个属性,所以它不会像编写Bid2 = Bid * 2那样简单。实际上,您需要找到特定marketlistobject的bid2,因此它更像Bid2 = prices[index].Bid * 2
例如。
组件.ts

@Component({
    selector: 'app-comprarzec',
    templateUrl: './comprarzec.component.html',
    styleUrls: [ './comprarzec.component.scss' ]
})
export class ComprarzecComponent implements OnInit {
    private prices: MarketListObject[] = [];

    constructor(private bittrexService: BittrexService) {
    }

    bid2(price: MarketListObject): number {
        return price.Bid * 2;
    }

    ngOnInit() {
        this.bittrexService.getPrices().subscribe(data => {
            this.prices = data.result;
        });
    }
}

comparzec.component.html压缩程序
<ul>
    <li *ngFor="let price of prices">
        {{price.Bid}}
        {{bid2(price)}}
    </li>
</ul>

不过进展顺利,因为你刚开始。人们通常会先被http的东西绊倒。

关于angular - Angular 4, typescript 将接口(interface)属性分配给变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44687186/

10-10 14:22