Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Service:
  2. postCliente(cliente: Clientes){
  3.         let bodypost = JSON.stringify(cliente);
  4.         let headers = new Headers({'Content-Type':'application/json'});
  5.         return this._http.post(this.url+'clientes',bodypost,{headers:headers})
  6.             .map(res => res.json())
  7.             .catch((err:Response)=> Observable.throw(err.json()));
  8.     }
  9.  
  10. Interface:
  11. export interface Clientes {
  12.     cemail: string;
  13.     cpassword: string;
  14.     cnombre: string;
  15.     capellidos: string;
  16.     ctelefono: string;
  17.     activo: string;
  18. }
  19.  
  20. Component:
  21. import { ActivatedRoute, Router } from '@angular/router/';
  22. import { ClientesService } from '../clientes.service';
  23. import { Clientes } from '../clientes';
  24. import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
  25. import { FormsModule } from '@angular/forms';
  26.  
  27. @Component({
  28.   selector: 'kp-cliente',
  29.   templateUrl: './cliente.component.html',
  30.   styleUrls: ['./cliente.component.css'],
  31.   providers:[ClientesService]
  32. })
  33. export class ClienteComponent implements OnInit {
  34.     public opcioncliente: string;
  35.     public isRequired: boolean;
  36.     public isDisabled: boolean;
  37.     public isDisabledMultiple: boolean;
  38.     public itemMultiple: any;
  39.     public Client: string;
  40.     public cliente: Clientes;
  41.     public errorMessage;
  42.  
  43.   constructor(private _clientesService:ClientesService,
  44.               private _route:ActivatedRoute,
  45.               private _router:Router) {
  46.         this.opcioncliente = 'nuevo cliente';
  47.         this.isRequired = true;
  48.         this.isDisabled = false;
  49.         this.isDisabledMultiple = false;
  50.         this.itemMultiple = null;
  51.  
  52.   }
  53.  
  54.   ngOnInit( ) {
  55.     this.cliente = new Clientes("","","","","","");
  56.   }
  57.  
  58.   public postCliente(){
  59.     console.log(this.cliente);
  60.     this._clientesService.postCliente(this.cliente).subscribe(
  61.       response=> {
  62.             if(!response.cliente){
  63.                 alert(`Error al guardar nuevo cliente `)
  64.             }else{
  65.             this.cliente = response.cliente;
  66.             }
  67.       },
  68.        error => {
  69.             this.errorMessage = <any>error;
  70.             if(this.errorMessage != null){
  71.                 console.log(this.errorMessage);
  72.                 alert(`Error al conseguir los clientes ${this.errorMessage}`);
  73.             }
  74.         }
  75.  
  76.     )
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement