英文:
Angular search by clicked tag
问题
在我的应用程序中,我正在尝试实现一个功能,当用户点击标签时,显示所有拥有此标签的产品...
我的搜索请求是通过 API 调用的 GET 方法进行的,所以我尝试实现的是,在单击标签时将标签值作为参数发送到 URL,并在新页面中返回具有该标签的所有产品... 我在 Postman 中使用 API 调用是有效的,但在 Angular 中实现时遇到了问题...
所以我的主要问题和困难是:
1)如何使标签可点击以便将值发送到 API 请求中
2)如何向标签添加 routerlink,使其重定向到显示具有此标签的所有产品的新页面
我对 Angular 很陌生,请帮帮我
这是应用中显示标签的图像:
以下是我的代码:
home.page.html 中用于输出标签的 HTML 代码:
<ion-chip *ngFor="let tag of tags">
<ion-label>{{ tag.tags }}</ion-label>
</ion-chip>
search.service.ts 中的搜索 API 代码:
searchByTagCall(tag: string) {
return from(Preferences.get({key: 'TOKEN_KEY'})).pipe(
switchMap(token => {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token.value}`);
let params = new HttpParams();
params = params.append('tag', tag);
return this.httpClient.get(`${environment.apiUrl}search`, {headers, observe: 'response', params});
}),
catchError(err => {
console.log(err.status);
if (err.status === 400) {
console.log(err.error.message);
}
if (err.status === 401) {
this.authService.logout();
this.router.navigateByUrl('/login', {replaceUrl: true});
}
return EMPTY;
}),
);
}
home.page.ts 中的代码:
searchByTag() {
this.searchService.searchByTagCall(tag).subscribe(
(data: any) => {
/* 在这里我应该放什么? */
},
error => {
console.log('Error', error);
});
}
我的 JSON 数据看起来像这样:
{
"tags": [
{
"tags": "fruit"
},
{
"tags": "sour"
},
{
"tags": "sweet"
},
{
"tags": "frozen"
},
{
"tags": "fresh"
},
{
"tags": "vegetable"
},
{
"tags": "raw"
}
]
}
英文:
In my app I am trying to make a feature that when the user click the tag it shows him all the products that have this tag...
My search request is being made with GET method over an API call... so what I am trying to achieve is that on a tag click the tag value is sent as a parameter in the url and thus returning all products with this tag in a new page... My API call works in POSTMAN but I am having trouble implementing it in Angular...
So my main questions and issues are:
- How to make the tag clickable so it sends the value with the api request
- How to add routerlink to the tag so it redirects to new page where it shows all the products with this tag
I am very new to Angular so please help
This is the image how tags are displayed in the app:
Here is my code:
HTML in home.page.html for outputing the tags:
<ion-chip *ngFor="let tag of tags">
<ion-label>{{ tag.tags }}</ion-label>
</ion-chip>
Code for search API in search.service.ts:
searchByTagCall(tag: string) {
return from(Preferences.get({key: 'TOKEN_KEY'})).pipe(
switchMap(token => {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token.value}`);
let params = new HttpParams();
params = params.append('tag', tag);
return this.httpClient.get(`${environment.apiUrl}search`, {headers, observe: 'response', params});
}),
catchError(err => {
console.log(err.status);
if (err.status === 400) {
console.log(err.error.message);
}
if (err.status === 401) {
this.authService.logout();
this.router.navigateByUrl('/login', {replaceUrl: true});
}
return EMPTY;
}),
);
}
Code of home.page.ts:
searchByTag() {
this.searchService.searchByTagCall(tag).subscribe(
(data: any) => {
/* what do I put here? */
},
error => {
console.log('Error', error);
});
}
My JSON looks like this:
{
"tags": [
{
"tags": "fruit"
},
{
"tags": "sour"
},
{
"tags": "sweet"
},
{
"tags": "frozen"
},
{
"tags": "fresh"
},
{
"tags": "vegetable"
},
{
"tags": "raw"
}
]
}
答案1
得分: 1
以下是翻译好的部分:
home.page.html:
<ion-chip *ngFor="let tag of tags">
<ion-label class="tag" (click)="searchByTag(tag.tags)">{{ tag.tags }}</ion-label>
</ion-chip>
home.page.scss:
// 如果需要将鼠标光标更改为手形图标
.tag {
cursor: pointer;
}
home.page.ts:
constructor(/*已有的构造函数参数*/, private router: Router, private dataService: DataService) {
// 构造函数中已有的代码
}
searchByTag(tag: string) {
this.searchService.searchByTagCall(tag).subscribe((data: any) => {
// 1. 将数据存储在某个服务中,以便其他页面可以访问
this.dataService.tagData = data;
// 2. 导航到其他页面(如果需要,可以将标签存储在路由参数中)
this.router.navigate(['/tagPage/', tag]);
},
error => {
console.log('Error', error);
});
}
英文:
Do the following changes:
home.page.html:
<ion-chip *ngFor="let tag of tags">
<ion-label class="tag" (click)="searchByTag(tag.tags)">{{ tag.tags }}</ion-label>
</ion-chip>
home.page.scss:
// In case you need to change the cursor to be the hand icon
.tag {
cursor: pointer;
}
home.page.ts:
constructor(/*whatever parameters you have in your constructor already*/, private router: Router, private dataService: DataService) {
// Whatever code you have in your constructor
}
searchByTag(tag: string) {
this.searchService.searchByTagCall(tag).subscribe((data: any) => {
// 1. Store the data in some service so it will be accessible for the other page
this.dataService.tagData = data;
// 2. Navigate to the other page (you can store the tag in the route params if you need it)
this.router.navigate(['/tagPage/', tag]);
},
error => {
console.log('Error', error);
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论