I'm trying to update a field on my application using the put method however I'm having some problems trying to do it. I'm new on Angular 2 and still dont know how to do it correctly.
Here is my Service:
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/disputas';
constructor(private http: Http){}
atualizaStatus (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)
.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
}
}
and here is the Component:
import { Component } from '@angular/core';
import {Http, Headers} from '@angular/http';
import {ActivatedRoute} from '@angular/router';
import {DisputaComponent} from '../../disputas/disputas.component';
import {DisputaService} from '../../disputas/disputas.service';
import {DisputaPropostaService} from './disputas-proposta.service';
import {disputaPropostas} from './proposta.interface';
@Component({
moduleId: module.id,
selector: 'detalhes',
templateUrl: `disputas-proposta.component.html`,
providers: [DisputaPropostaService]
})
export class DisputaPropostaComponent {
disputa: DisputaComponent;
service: DisputaService;
propostaService:DisputaPropostaService;
route: ActivatedRoute;
inputProposta = false;
proposta:disputaPropostas = {proposta_usuario: null, proposta_cliente: null}
constructor(service: DisputaService, route:ActivatedRoute, propostaService:DisputaPropostaService){
// Some other code here...
}
recusaProposta(){
if (this.disputa.propostas_realizadas == 0){
this.propostaService.atualizaStatus(this.disputa)
.subscribe(
res => console.log(res),
error=> console.log(error)
);
}
this.inputProposta = true;
this.disputa.propostas_realizadas++;
if ((this.disputa.propostas_realizadas) == ((this.disputa.maximo_propostas)-1))
alert("Ultima proposta")
}
The message I'm getting is the following one:
Cannot read property 'atualizaStatus' of undefined
Am I missing something obvious? Thank you in advance.
PS: if someone has an article that explains the REST for Angular 2 I'll be glad to know :)
This is the correct way to implement dependency injection
Remove
service: DisputaService;
propostaService:DisputaPropostaService;
route: ActivatedRoute;
Update
constructor(private service: DisputaService,private route:ActivatedRoute, private propostaService:DisputaPropostaService){
// Some other code here...
}
Which has the same result as Bougarfaoui El houcine answer
I found this book to be a lot of help. https://angular-2-training-book.rangle.io/handout/di/
the problem is in your component constructor
constructor(service: DisputaService, route:ActivatedRoute, propostaService: DisputaPropostaService){
this.service = service;
this.route = route;
this.propostaService = propostaService;
// ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.