无法通过从表单传递参数获取API数据。

huangapple go评论67阅读模式
英文:

Cannot get data from API by passing parameters from form

问题

I have an API that gets data from API by passing parameters, but I can't pass the parameters to it from form:

  • 我的 API:
    https://api.myapi.com/books?categor=[list of values]&creation_date=value

  • 我的接口:

export interface Books{
    id: int
    name: string
    categor: string
    creation_date: string
}
  • 我的服务:
import { Books } from '../interfaces/books ';

constructor(private http:HttpClient) { }

getBooks(categor: string, creation_date: string): Observable<Books>{
    return this.http.get<Books>(`https://api.myapi.com/books?categor=${this.categor}&amp;creation_date=${this.creation_date}`).pipe(
      tap(books=> console.log(books)),
    );
}
  • 我的 component.ts:
books: any;
constructor(private booksService: BooksService){ }

onGetBooks(data:{categor: string, creation_date: string})
{
    this.bookService.getBooks(data).subscribe((response) => {
      console.log(response);
      this.books = response.list;
    })
}
  • 我的 HTML 代码:
<form #bookForm="ngForm" (ngSubmit)="onGetBooks(bookForm.value)">
  <mat-form-field>
    <mat-label>Input</mat-label>
    <input matInput name="categor" ngModel>
  </mat-form-field>
  <mat-form-field>
    <mat-label>Input</mat-label>
    <input matInput name="creation_date" ngModel>
  </mat-form-field>
  <button mat-raised-button>Get Books</button>
</form>
英文:

I have an API that gets data from API by passing parameters, but I can't pass the parameters to it from form:

  • My API :
https://api.myapi.com/books?categor=[list of values]&amp;creation_date=value
  • My interface:
export interface Books{
    id: int
    name: string
    categor: string
    creation_date: string
}
  • My Service :
import { Books } from &#39;../interfaces/books &#39;;

constructor(private http:HttpClient) { }

  getBooks(categor: string, creation_date: string): Observable&lt;Books&gt;{
    return this.http.get&lt;Books&gt;(`https://api.myapi.com/books?categor=${this.categor}&amp;creation_date=${this.creation_date}`).pipe(
      tap(books=&gt; console.log(books)),
    );
  }
  • My component.ts :
books: any;
constructor(private booksService: BooksService){ }

onGetBooks(data:{categor: string, creation_date: string})
  {
    this.bookService.getBooks(data).subscribe((response) =&gt; {
      console.log(response);
      this.books = response.list;
    })
  }
  • My HTML code :
&lt;form #bookForm =&quot;ngForm&quot; (ngSubmit)=&quot;onGetBooks(bookForm.value)&quot;&gt;
  &lt;mat-form-field&gt;
    &lt;mat-label&gt;Input&lt;/mat-label&gt;
    &lt;input matInput name=&quot;categor&quot; ngModel&gt;
  &lt;/mat-form-field&gt;
  &lt;mat-form-field&gt;
    &lt;mat-label&gt;Input&lt;/mat-label&gt;
    &lt;input matInput name=&quot;creation_date&quot; ngModel&gt;
  &lt;/mat-form-field&gt;

  &lt;button mat-raised-button&gt;Get Books&lt;/button&gt;

&lt;/form&gt;

答案1

得分: 1

需要对您的代码进行一些更改

getBooks(categor: string, creation_date: string): Observable&lt;Books&gt;{
return this.http.get&lt;Books&gt;(`https://api.myapi.com/books?categor=${categor}&amp;creation_date=${creation_date}`).pipe(
  tap(books=&gt; console.log(books)),
);
}

在服务文件中,您不需要使用 this,因为您将值作为函数参数传递。

如果仍然遇到调用API的问题,
要么将标签 &lt;button&gt; 更改为 &lt;input type=&quot;submit&quot;&gt;
要么将表单的事件触发器 (ngSubmit) 更改为 &lt;button&gt; to (click)

英文:

You need to make couple of changes in you code

getBooks(categor: string, creation_date: string): Observable&lt;Books&gt;{
return this.http.get&lt;Books&gt;(`https://api.myapi.com/books?categor=${categor}&amp;creation_date=${creation_date}`).pipe(
  tap(books=&gt; console.log(books)),
);
}

In service file you don't need to use this as you are passing values to service file as the function arguments.

if you still face issue on calling API change,
either change tag &lt;button&gt; to &lt;input type=&quot;submit&quot;&gt;
or change event trigger of form (ngSubmit) to &lt;button&gt; to (click)

huangapple
  • 本文由 发表于 2023年6月12日 01:24:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定