2017-03-08 7 views
0

私は、更新していないjsonを初めて更新するボタンをクリックします。何もJSONに起こりませんが、私はそれをクリックした場合は、再度、それは私が(disputa.propostas_realizadasサーバを更新するために2回クリックする必要があります - Angular2

をしたいフィールドを更新しない、私はそれをクリックして初めてで、今

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

:問題のボタンは、この関数を呼び出します

サービスは

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 
    } 
} 

私はお手伝いできますか?前もって感謝します。

答えて

0

あなたの関数では、disputa.propostas_realizadasを1だけ増やす前に返すのがその理由です。以下のコードで関数を置き換えてください。

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

答えに感謝し、それはまだ私が初めてで 'disputa.propostas_realizadas'と、このアップデートを印刷するように求めているが、JSONが更新されていないボタンをクリックした直後に、:(機能しません。 :(でも、とにかくありがとう –

関連する問題