简体   繁体   中英

How to pass parameters into services with Angular4

i need to pass some parameter to my services, please let me know where should i build the parameter: In the component part? or In the service part? and also suggest to make url-parameters which can be send to my php server, my component.ts file for get the all required list from my API:

transaction:any;
myList(transNo){
this.service.listOfData(transNo).subscribe(
  (data) => {
    this.source.load(data.records);
    this.dataLoaded = true;
  },
);
}
ngOnInit() {
this.settings = Object.assign({}, this.WorkshopDetailsSetting);
this.listTransaction(this.transaction);
this.optionsSelect = [
  { value: '1', label: 'Transaction 1' },
  { value: '2', label: 'Transaction 2' },
  { value: '3', label: 'Transaction 3' },
  { value: '4', label: 'Transaction 4' },
  { value: '5', label: 'Transaction 5' },
  { value: '6', label: 'Transaction 6' }
]

}

and my service.ts is as follows:

listOfData(data): Observable<any> {
    return this.http.get(`${this.API_ROOT}${this.API_LIST}`, data)
        .map(
        (res: Response) => res.json() || {},
    ).catch(this.handleError);
}

Please suggest how should i make parameter based param from component to services or services to API.

So your service should be something like this,

listOfData(data): Observable<any> {
        const url = `${environment.apiUrl}/getList?trans=${data}`;      
        const headers = new Headers();
        const options = new RequestOptions({ headers: headers });
        return this.http.get(url, options).map(
           (res: Response) => res.json() || {},
           ).catch(this.handleError);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM