callback - Angular subscribe, how do I make a method have a subscription -


i'm new angular 2 , not sure how subscriptions. have member service looks (simple version):

import { injectable, ngzone, output, eventemitter } '@angular/core'; import { network } '@ionic-native/network'; import { httpprovider } '../providers/http-provider';  @injectable() export class member {    firstname:string = "";   surname:string = "";    constructor(private httpprovider: httpprovider, private network: network, zone: ngzone) {   }    createmember(member){     this.httpprovider.createmember(member).subscribe(       result => {         var data = json.parse( result['_body'] );         this.firstname = member.firstname;         this.surname = member.surname;         return true;       },       err =>{         console.error("error : "+err);         return false;       }     );   } } 

then have component uses member service , want have like:

this.member.createmember(member).subscribe(data).... 

so can check member created successfully.

i this

component

 this.membersservice.createmember(member)      .subscribe(res => {          console.log('success', res)         }, err => {           console.log('error', err);         }); 

service

import {injectable} '@angular/core'; import {http, requestoptions, response, headers} '@angular/http'; import 'rxjs/rx';    createmember(member) {     const body = json.stringify(member);     const headers = new headers({       'content-type': 'application/json'     });     const options = new requestoptions({headers: headers});      return this.http.post('/api/members', body, options)       .map((res: response) => res.json())       .catch((err: any) => console.log(err));   } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -