如何将 :id 发送到服务 url of angular 2

how to send :id to service url of angular 2

我是 angular 2 的新手。 你们能帮帮我吗

我有这样的场景,

用户通过输入投诉 ID 获取投诉详情。

我有这样的 url :

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

我们需要将此 ID 发送到 server.so,以便我们获取数据。

通过手动传递我正在获取数据

我的代码:

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{

 }
}

只需将 id 作为参数传递给 getCompliants 函数并将其附加到 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());
    }
}

我想你在 url 中寻找 angular 2 传递参数,请参考这个 link : 为此,您可以为 angular 2:

编写如下代码

在您的场景中,您可以使用如下所示的路由器代码:

{
路径:'home/promotiondetail',
组件:组件名称

}

在您的 html 端,您可以声明您的路由器代码如下所示:

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

你也可以参考这个 URL :