如何转换存储在Observable< any>中的值到一个字符串,在打字稿? [英] How to convert the value stored in Observable<any> to a string, in typescript?

查看:51
本文介绍了如何转换存储在Observable< any>中的值到一个字符串,在打字稿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular和TypeScript的新手.我需要一个字符串格式的Observable值,这怎么办?

Hi I am new to Angular and TypeScript. I need the value of an Observable in the format of a string, how does one do this?

BmxComponent文件

the BmxComponent file

export class BmxComponent {
    asyncString = this.httpService.getDataBmx();
    currentStock = this.httpService.getDataBmx2(); //this is what I want to covert to a string so I can pass it to onSubmit()

    onSubmit() {
        const asnNumm = this.currentStock; // passing it here changes my database, see below
        this.httpService.sendData({ stock: asnNumm })
            .subscribe(
            data => console.log(data),
            error => console.log(error)
            );
    }
}

HttpService文件

the HttpService file

export class HttpService {

    constructor(private http: Http) {}

    getDataBmx() {
        return this.http.get('https://the-bicycle-shop.firebaseio.com/products/Bicycles/bmx/stock.json')
            .map((response: Response) => response.json());
    }

    getDataBmx2() {
        return (this.http.get('https://the-bicycle-shop.firebaseio.com/products/Bicycles/bmx/stock.json'));
    }   

    sendData(newStock: any) {
        const body = JSON.stringify(newStock);
        const headers = new Headers();
        headers.append('Content-Type', 'application/json');
        return this.http.patch('https://the-bicycle-shop.firebaseio.com/products/Bicycles/bmx.json', body, {
            headers: headers
        })
            .map((data: Response) => data.json())
            .catch(this.handleError);
    }

    private handleError(error: any) {
        console.log(error);
        return Observable.throw(error.json());
    }
}

html文件

<p>{{asyncString | async}}</p> // displays 1234 which is the correct value
<p>{{asyncString}}</p> // displays [object Object]
<p>{{currentStock}}</p> // displays [object Object]
<button class="btn btn-success" (click)="onSubmit()">Change Database</button>

onSubmit()之前的我的数据库(当我单击更改数据库"按钮时使用)

my database before onSubmit() (used when I click the Change Database button)

Bicycles
|
---bmx
    |
    ---stock = 1234;

onSubmit()之后的我的数据库

my database after onSubmit()

Bicycles
|
--- bmx
     |
     ---stock
         |
         --- _isScalar = false

我正在为此使用Firebase.

I am using Firebase for this.

我知道它可以与字符串一起使用,因为我是这样测试的:

I know it will work with a string because I tested it with like this:

    onSubmit() {
        const asnNumm = "33333" //the change to test it
        this.httpService.sendData({ stock: asnNumm })
            .subscribe(
            data => console.log(data),
            error => console.log(error)
            );
    }

这对我的数据库做了

Bicycles
|
---bmx
    |
    ---stock = 33333

我知道currentStock将拥有与数据库中当前存储的相同的值,因此不会有什么区别,但是一旦将其转换为字符串后,我想对其进行更改.

I understand that currentStock would hold the same value that is currently stored in my database, so it would make no difference, but I want to change it once I have converted it to a string.

基本上,我想更改数据库中的库存",但是每次我按更改数据库"按钮时,都要更改固定的数量,例如,每次按下时将其减去1.

Basically I want to change "stock" in my database, but by a fixed amount each time I press the Change Database button, for example, minus 1 it each time it is pressed.

推荐答案

订阅可观察对象以获取结果,并在收到值时调用onSubmit:

Subscribe to the observable to get the result and call onSubmit when you receive the value:

currentStock = this.httpService.getDataBmx2()
.subscribe(val => this.onSubmit(val));

这篇关于如何转换存储在Observable&lt; any&gt;中的值到一个字符串,在打字稿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆