Angular 服务 return 请求的 url 中参数 ID 的无效数据 "undefined"

Angular service return Invalid data "undefined" for parameter id in url requested

我有一个在组件中调用的服务,但是 returns 错误 400 错误请求和消息

Invalid data "undefined" for parameter id

知道 api 中的方法 getProduct 完美运行。

Service.ts

getProduct(ID: number): Observable<any> {
  // console.log('ID', ID)
  return this.http.get(environment.api+'/products/' + JSON.stringify(ID)) as Observable<Product[]>;
}

component.ts

searchIdAction(id: number){
  return this.venteService.getProduct(id).subscribe(
    product => {
      // console.log('product', product);
      this.product = product;
    },
  )
}

component.html

<clr-input-container *ngIf="options == 1">
  <label>Recherche:</label>
  <input clrInput placeholder="recherche  par id ..." type="number"  (keyup.enter)="searchIdAction(modelProduct.id)"/>
  <!-- name="name" [(ngModel)]="modelProduct.id"   -->
</clr-input-container>

需要您的帮助。提前致谢

根据您的代码,我会说您的输入缺少一些模型绑定,因此,您的 id 始终未定义。 Angular 无法知道您想将输入的值放入 modelProduct.id

您应该使用输入下方注释的 ngModel,或者如果您不需要存储此值,则直接访问输入值:

<input #myInput clrInput placeholder="recherche  par id ..." type="number"  (keyup.enter)="searchIdAction(myInput.value)"/>

已解决。希望能帮到别人

<input name="idInput" [(ngModel)]="idInput" type="text" (keyup)="searchProduct(idInput)" clrInput placeholder="recherche  par nom ..." />