Ajay
Ajay

Reputation: 307

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{

 }
}

Upvotes: 1

Views: 3832

Answers (2)

Ravi
Ravi

Reputation: 149

I think you looking For angular 2 pass parameter in url please refer this link : For that you can code look like this for angular 2:

In your scenario you can use code for router look like this :

{
path: 'home/promotiondetail',
component: component name

}

and in your html side you can declare your router code look like this :

[routerLink]="['promotiondetail']" [queryParams]="{ id: {{id}} }"

You can also refer this URL :

How to use questionmark in URl using angular 2

Upvotes: 1

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41397

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());
    }
}

Upvotes: 1

Related Questions