stackoverflow account
stackoverflow account

Reputation: 235

Ionic- Angular : Pass data to a service

I have to pass data to my service and embed it to a url string. Below is how i am receiving data in my constructor while user navigates to the page :

  constructor(public alertController: AlertController,
              private route: ActivatedRoute,private ticket_details: TicketDetailsService) {
                this.route.queryParams.subscribe((data)=>{
                  this.id=JSON.stringify(data);
                  alert(this.id);
                });
        }

Now here i am calling a service with following code

 tcketDetails(){
       this.ticekt_details.get_ticket_details().then(ticketData => {
        console.log(ticketData.data);
        this.ticketData=ticketData.data;
      });

  }

Service Code::

private server_path = 'https://example.com/apis/tickets/ticekt_details/id';

 constructor(private http: HTTP) {  }
 async get_ticket_details() {
    return  this.http.get(this.server_path, {}, {})
      .then(data => {
        return JSON.parse(data.data);
      })
      .catch(error => {
        console.log(error.status);

      });

  }

After receiving id as seen in alert how do I pass id parameter to server_path variable in my service , Please guide

Upvotes: 0

Views: 318

Answers (1)

Gaurang Dhorda
Gaurang Dhorda

Reputation: 3387

In constructor of component after this line this.id=JSON.stringify(data); when You set Id, call below function to update URL of sertvice like below, You need to add this function in service as well.

In constructor(), call below function to update url..

this.id=JSON.stringify(data);
this.ticket_details.updateUrl(id);

and then inside ticket_details add function definition to update your url before calling get_ticket_details() function.

so updateUrl() function seems like this ...

updateUrl(id){
    this.server_path = `this.server_path/ + ${id}`;
}

and, at top of your service this.serverUrl should be like below...

private server_path = 'https://example.com/apis/tickets/ticekt_details';

Now, You can proceed to call get_ticket_details().

Upvotes: 1

Related Questions