Im學習Angular,我正在創建一個加密貨幣交換應用。我創建了一個服務和一個接口來從API獲取數據。現在我可以將它綁定到DOM,但我也想在我的component.ts中使用這些數據,以便我可以編寫例如:Angular 4,打字稿將接口屬性分配給變量
Bid2 = Bid * 2;
,然後綁定變量,以這樣的DOM:{{標段2}}
感謝您的幫助。這裏是我的代碼:
Component.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
);
}
}
Service.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());
}
}
interface.ts(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;
}
再次感謝您的幫助!
製作ComprarzecComponent的公共屬性,並將其稱爲Bid2。該模板應該能夠綁定到它。當您更改該值時,模板應該注意。 –
在插值中,您也可以進行數學運算:{{Bid * 2}} –