Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Service:
- postCliente(cliente: Clientes){
- let bodypost = JSON.stringify(cliente);
- let headers = new Headers({'Content-Type':'application/json'});
- return this._http.post(this.url+'clientes',bodypost,{headers:headers})
- .map(res => res.json())
- .catch((err:Response)=> Observable.throw(err.json()));
- }
- Interface:
- export interface Clientes {
- cemail: string;
- cpassword: string;
- cnombre: string;
- capellidos: string;
- ctelefono: string;
- activo: string;
- }
- Component:
- import { ActivatedRoute, Router } from '@angular/router/';
- import { ClientesService } from '../clientes.service';
- import { Clientes } from '../clientes';
- import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
- import { FormsModule } from '@angular/forms';
- @Component({
- selector: 'kp-cliente',
- templateUrl: './cliente.component.html',
- styleUrls: ['./cliente.component.css'],
- providers:[ClientesService]
- })
- export class ClienteComponent implements OnInit {
- public opcioncliente: string;
- public isRequired: boolean;
- public isDisabled: boolean;
- public isDisabledMultiple: boolean;
- public itemMultiple: any;
- public Client: string;
- public cliente: Clientes;
- public errorMessage;
- constructor(private _clientesService:ClientesService,
- private _route:ActivatedRoute,
- private _router:Router) {
- this.opcioncliente = 'nuevo cliente';
- this.isRequired = true;
- this.isDisabled = false;
- this.isDisabledMultiple = false;
- this.itemMultiple = null;
- }
- ngOnInit( ) {
- this.cliente = new Clientes("","","","","","");
- }
- public postCliente(){
- console.log(this.cliente);
- this._clientesService.postCliente(this.cliente).subscribe(
- response=> {
- if(!response.cliente){
- alert(`Error al guardar nuevo cliente `)
- }else{
- this.cliente = response.cliente;
- }
- },
- error => {
- this.errorMessage = <any>error;
- if(this.errorMessage != null){
- console.log(this.errorMessage);
- alert(`Error al conseguir los clientes ${this.errorMessage}`);
- }
- }
- )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement