Search code examples
angularjsangular2-routingangular2-formsangular2-services

how to send :id to service url of angular 2


I am new to angular 2. can you guys help me out.

i have scenario like this,

user fetches complaint details by entering complaint id.

i have the url like this :

http://192.168.0.106:8000/app/complaint/complaintstatus/:id

this id we need to send to server.so that we can fetch the data.

by passing manually i am fetching the data

my code:

export class ComplaintService{
    private _url:string ="http://192.168.0.106:8000/app/complaint/complaintstatus"
    constructor(private _http:Http){}
    getCompliants(){
        return this._http.get(this._url).map((response:Response)=>response.json());
    }
}


export class ComplaintStatusComponent implements OnInit {

  constructor(private _complaintService:ComplaintService) { }
  complaints={};

  ngOnInit() {
    this.getCompliants();
  }
  getComplaintDetails(complntId:any){

    console.log(complntId);
    this._complaintService.getCompliants().subscribe(data => this.complaints=data);
 }
 getCompliants():void{

 }
}

Solution

  • Just pass the id as argument to the getCompliants function and append it to the url

     getComplaintDetails(complntId:any){ 
        console.log(complntId);
        this._complaintService.getCompliants(complntId).subscribe(data => this.complaints=data);
     }
    
    export class ComplaintService{
        private _url:string ="http://192.168.0.106:8000/app/complaint/complaintstatus"
        constructor(private _http:Http){}
        getCompliants(complntId){
            return this._http.get(this._url + '/:' + complntId).map((response:Response)=>response.json());
        }
    }