繁体   English   中英

需要单击两次以更新服务器-Angular2

[英]Need to click twice to update server - Angular2

我有一个第一次更新的按钮未更新的json。 有问题的按钮调用此功能:

recusaProposta(){
    this.propostaService.atualizaDisputa(this.disputa)
    .subscribe(
      res => this.disputa.propostas_realizadas++,
      error => console.log(error)
    );
}

现在,第一次单击它时,json上没有任何反应,但是如果再次单击它,它会更新我想要的字段( disputa.propostas_realizadas

这是服务:

import {Injectable} from '@angular/core';
import {Http, Headers, Response, RequestOptions} from '@angular/http';
import {Component} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {DisputaPropostaComponent} from './disputas-proposta.component';
import 'rxjs/add/operator/map';

@Injectable()
export class DisputaPropostaService{

    contato:Object[] = [];
    name: string;
    headers:Headers;
    url: string = 'http://localhost:3004/disputa';

    constructor(private http: Http){}

    atualizaDisputa (body:any): Observable<DisputaPropostaComponent[]>{
        let bodyString = JSON.stringify(body); // Stringify payload
        let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
        let options       = new RequestOptions({ headers: headers }); // Create a request option
        return this.http.put(`${this.url}/${body['id']}`, body, options) // ...using post request
                         .map((res:Response) => res.json()) // ...and calling .json() on the response to return data
                         .catch((error:any) => Observable.throw(error.json().error || 'Ocorreu um erro em nosso servidor, tente novamente mais tarde')); //...errors if any
    }
}

你们能帮我吗? 提前致谢。

原因是在函数中您将disputa.propostas_realizadas返回1,然后再将其增加1。将函数替换为下面的代码,它应该可以工作。

recusaProposta(){
    this.propostaService.atualizaDisputa(this.disputa)
    .subscribe(
      res => ++this.disputa.propostas_realizadas,
      error => console.log(error)
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM