Reputation: 525
I feel like this is an appropriate situation to ask this and I have read, so please don't bash me. (Are "Is it a good practice...?" questions off topic or opinion-based?)
MY QUESTION: What is the alternative (better practice) for passing a parameter from a component.ts to to a service.ts that is to be used to define the URL. (ex. param value =123 , URL: www.blogs.com/posts/"add params here")
Background: A users form has a name field, and zipcode field. Based off of the users zipcode, they will see different Providers. What I did was took the zipcode value from the form and passed it from one function to the next over to the service.ts. From there is just added that value to the URL string ex... user 1 ,zipcode =12345 zip= user.zipcode data_service = url = "www.blog.com/post/+ val" + zip
. So this works, but I feel like this is a bad practice.
:CODE
component.ts
(this part starts from the code that loads the users info)
...
const zipParam = this.patientDetails.patient_zipcode
console.log(zipParam)
this.getProvider(zipParam);
}
}, err => {
console.log(err);
});
}
getProvider(zip){
console.log("fZip:",zip)
this.reqObj.zip = zip
this.dss.getProvider(this.reqObj,zip).subscribe(res => {
console.log("Checkres",res);
this.serviceProvider = res
}, err =>{
console.log(err);
});
};
Service.ts
getProvider(reqObj,zip): Observable<Object>{
console.log("dZip",zip)
return this.http.get("https://blog.com"+ zip + "&radius=1").map(res => res);
};
}
Upvotes: 2
Views: 531
Reputation: 92457
You can use Named Routes and some helper static class to easy use it.
Upvotes: 1