英文:
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}&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]&creation_date=value
- My interface:
export interface Books{
id: int
name: string
categor: string
creation_date: string
}
- My Service :
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}&creation_date=${this.creation_date}`).pipe(
tap(books=> 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) => {
console.log(response);
this.books = response.list;
})
}
- My HTML code :
<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>
答案1
得分: 1
需要对您的代码进行一些更改
getBooks(categor: string, creation_date: string): Observable<Books>{
return this.http.get<Books>(`https://api.myapi.com/books?categor=${categor}&creation_date=${creation_date}`).pipe(
tap(books=> console.log(books)),
);
}
在服务文件中,您不需要使用 this
,因为您将值作为函数参数传递。
如果仍然遇到调用API的问题,
要么将标签 <button>
更改为 <input type="submit">
,
要么将表单的事件触发器 (ngSubmit) 更改为 <button>
to (click)
英文:
You need to make couple of changes in you code
getBooks(categor: string, creation_date: string): Observable<Books>{
return this.http.get<Books>(`https://api.myapi.com/books?categor=${categor}&creation_date=${creation_date}`).pipe(
tap(books=> 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 <button>
to <input type="submit">
or change event trigger of form (ngSubmit) to <button>
to (click)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论