Angular按点击的标签搜索

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

Angular search by clicked tag

问题

在我的应用程序中,我正在尝试实现一个功能,当用户点击标签时,显示所有拥有此标签的产品...

我的搜索请求是通过 API 调用的 GET 方法进行的,所以我尝试实现的是,在单击标签时将标签值作为参数发送到 URL,并在新页面中返回具有该标签的所有产品... 我在 Postman 中使用 API 调用是有效的,但在 Angular 中实现时遇到了问题...

所以我的主要问题和困难是:
1)如何使标签可点击以便将值发送到 API 请求中
2)如何向标签添加 routerlink,使其重定向到显示具有此标签的所有产品的新页面

我对 Angular 很陌生,请帮帮我 Angular按点击的标签搜索

这是应用中显示标签的图像:

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:

  1. How to make the tag clickable so it sends the value with the api request
  2. 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 Angular按点击的标签搜索

This is the image how tags are displayed in the app:
Angular按点击的标签搜索

Here is my code:
HTML in home.page.html for outputing the tags:

  &lt;ion-chip *ngFor=&quot;let tag of tags&quot;&gt;
    &lt;ion-label&gt;{{ tag.tags }}&lt;/ion-label&gt;
  &lt;/ion-chip&gt;

Code for search API in search.service.ts:

  searchByTagCall(tag: string) {
    return from(Preferences.get({key: &#39;TOKEN_KEY&#39;})).pipe(
      switchMap(token =&gt; {
        const headers = new HttpHeaders().set(&#39;Authorization&#39;, `Bearer ${token.value}`);
        let params = new HttpParams();
        params = params.append(&#39;tag&#39;, tag);
        return this.httpClient.get(`${environment.apiUrl}search`, {headers, observe: &#39;response&#39;, params});
      }),
      catchError(err =&gt; {
        console.log(err.status);
        if (err.status === 400) {
          console.log(err.error.message);
        }
        if (err.status === 401) {
          this.authService.logout();
          this.router.navigateByUrl(&#39;/login&#39;, {replaceUrl: true});
        }
        return EMPTY;
      }),
    );
  }

Code of home.page.ts:

  searchByTag() {
    this.searchService.searchByTagCall(tag).subscribe(
      (data: any) =&gt; {
        /* what do I put here? */
      },
      error =&gt; {
        console.log(&#39;Error&#39;, error);
      });
  }

My JSON looks like this:

{
  &quot;tags&quot;: [
    {
      &quot;tags&quot;: &quot;fruit&quot;
    },
    {
      &quot;tags&quot;: &quot;sour&quot;
    },
    {
      &quot;tags&quot;: &quot;sweet&quot;
    },
    {
      &quot;tags&quot;: &quot;frozen&quot;
    },
    {
      &quot;tags&quot;: &quot;fresh&quot;
    },
    {
      &quot;tags&quot;: &quot;vegetable&quot;
    },
    {
      &quot;tags&quot;: &quot;raw&quot;
    }
  ]
}

答案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:

&lt;ion-chip *ngFor=&quot;let tag of tags&quot;&gt;
    &lt;ion-label class=&quot;tag&quot; (click)=&quot;searchByTag(tag.tags)&quot;&gt;{{ tag.tags }}&lt;/ion-label&gt;
&lt;/ion-chip&gt;

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) =&gt; {
        // 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([&#39;/tagPage/&#39;, tag]); 
    },
    error =&gt; {
        console.log(&#39;Error&#39;, error);
    });
}

huangapple
  • 本文由 发表于 2023年2月8日 20:24:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385755.html
匿名

发表评论

匿名网友

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

确定