【问题标题】:NgFor/NgIf multiply selection(favorites)NgFor/NgIf 多重选择(收藏夹)
【发布时间】:2021-08-02 18:06:01
【问题描述】:

我有一个天气应用程序,我想在其中用“全心”图标标记最喜欢的城市。

目前,当我点击不同的城市时,之前的城市没有被标记(有点问题),

所以基本上我只显示一个“全心”图标,而不是几个。

感谢修改我的应用程序的帮助。

添加和删除收藏夹按预期工作(显示在“收藏夹”路线中),但我想不通

了解如何将它与最喜欢的图标结合起来。

https://stackblitz.com/edit/github-w3expd?file=src/app/weather-page/weather-page.component.html

Component.ts

setLikedCity(index:any){
    this.likedCity = index
  }
  

addToFav(name:any, local:any){
    let favCity = {
        name:name,
        local:local
    }
    console.log("local:", local, "name:", name)
    console.log("this.favoriteLocations:", this.favoriteLocations)
    
        if(this.favoriteLocations.length ==0){
            this.favoriteLocations.push(favCity)
            console.log("this.favoriteLocations:", this.favoriteLocations)
            console.log("ADDED")
        }
        
        else if(this.favoriteLocations.length > 0){
                let index;
                for(let i =0; i<this.favoriteLocations.length; i++){
                    if(this.favoriteLocations[i].name.Key ==favCity.name.Key){
                        index = i
                        // break;
                    }
                }
                
                if(index !=null){
                    this.favoriteLocations.splice(index, 1)
                    this.likedCity = ''
                    console.log("REMOVED")
                
                }else{
                    this.favoriteLocations.push(favCity)
                    console.log("ADDED")
                    }
                console.log("this.favoriteLocations:", this.favoriteLocations)
        }
        this.weatherService.favoriteLocations =  this.favoriteLocations
}

Component.html

<div class="container">
    <div class="small-container">
        <div class="row mt-5">
            <div class="card" style="width: 18rem;" *ngFor="let name of TelAvivSearch; let i = index;">
                <div class="card-body" style="text-align: center;">
                    <h5 class="card-title ">{{name.LocalizedName}}</h5>
                    <div *ngFor="let local of telAvivLocal ">
                        <p class="card-text ">{{local.WeatherText}}</p>
                        <p class="card-text ">{{local.Temperature.Metric.Value}}°</p>
                        <div *ngIf="i!==likedCity">
                            <i class="far fa-heart" (click)="addToFav(name, local);setLikedCity(i)"></i>
                        </div>
                        <div *ngIf="i===likedCity">
                            <i class="fas fa-heart" (click)="addToFav(name, local);"></i>
                        </div>
                    </div>
                </div>

            </div>

        </div>
    </div>
    <!-- favorites -->
    <div class="forecast-container mt-3 ">
        <h1 style="text-align: center; ">Forecast</h1>
        <div class="small-container ">
            <div class="row">
                <div *ngFor="let forecast of Forecast.DailyForecasts">
                    <div class="card " style="width: 13rem; ">
                        <div class="card-body" style="text-align: center;">
                            <h5 class=" card-title ">{{forecast.Date | date}} </h5>
                            <p class="card-text ">Day: {{forecast.Day.IconPhrase}}</p>
                            <p class="card-text ">Night: {{forecast.Night.IconPhrase}}</p>

                            <p class="card-text ">{{forecast.Temperature.Maximum.Value}}°<span>/ {{forecast.Temperature.Minimum.Value}}°</span></p>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

    标签: angular ngfor angular-ng-if


    【解决方案1】:

    您可以在 yout commponent.ts 中添加以下方法并更改 html 端的 *ngIf 条件 - 它现在应该可以工作了。

      isLikedCity(key: number): boolean {
        const idx = this.favoriteLocations.findIndex(
          x => x.name['Key'] === key
        );
        return idx >= 0;
      }
      <div *ngIf="!isLikedCity(name.Key)">
           <i class="far fa-heart" (click)="addToFav(name, local);setLikedCity(i)"></i>
    </div>
    <div *ngIf="isLikedCity(name.Key)">
          <i class="fas fa-heart" (click)="addToFav(name, local);"></i>
    </div>

    【讨论】:

    • 请记住,模板表达式上下文中的函数调用将被多次调用,并且可能是性能问题。
    【解决方案2】:

    如果我是你,那么我可能会为 TelAvivSearch 对象数组的所有对象添加一个附加属性,称之为 blFavorite - 这将通过按下心脏来改变它的值。如果满足条件,也会在 html 端完整显示 *ngIf=name.blFavorite.

    【讨论】:

    • 我愿意,但它来自天气 API 的响应,每天只给你 50 个请求(这没什么),所以我必须保存响应并在本地工作,然后再使用它再次使用 API。
    • 嗯,我明白了,在这种情况下,我建议尝试我在下面写的方法(如果它适合你,请告诉我)。
    • 成功了,谢谢!就一个问题。城市的名称不是唯一的,但它的 Key 是 (""Key":"3",)。我尝试将 name 替换为 key,但单击时出现错误:ERROR TypeError: Cannot read property 'Key' of undefined 你能试试吗?(name.Key)
    • HTML 将给定参数更改为 Key:isLikedCity(name.Key),在 TS 中修改以下行:x.name['Key']。另外不要忘记更改 isLikedCity(name: number) 的输入参数类型
    • 我修改了上面的代码,应该可以了。
    【解决方案3】:

    我所做的是: 为TelAvivSearch 中的所有元素添加了一个新键Liked: 0,,如下所示:

    {
            "Version":1,
            "Key":"3",
            "Type":"City",
            "Rank":31,
            "LocalizedName":"Herzeliya",
            "Liked":0,              <<< new key 'Liked' added
            "Country":{
                "ID":"IL",
                "LocalizedName":"Israel"
            },
            "AdministrativeArea":{
                "ID":"TA",
                "LocalizedName":"Tel Aviv"
            }
        },
    

    当用户点击心形图标 - 在setLikedCity 我设置this.TelAvivSearch[index].Liked = 1; 如下:

     setLikedCity(index:any){
        this.TelAvivSearch[index].Liked = 1;
     }
    

    addToFav 函数中,根据项目是否从收藏列表中添加/删除来设置Liked 的值,如下所示:

                .....
                    if(index !=null){
                        this.favoriteLocations.splice(index, 1)
                        this.TelAvivSearch[index].Liked = 0;
                        console.log("REMOVED")
                .....
    

    html文件中,根据Liked这个键值选择的心形图标显示为

    <div *ngIf="name.Liked === 1">
        <i class="fas fa-heart" (click)="addToFav(name, local);"></i>
    </div>
    

    它按预期工作。

    【讨论】:

    • 我会这样做,但目前我在本地处理来自我保存的 API 的响应,因为该 API 每天只允许 50 个请求。
    猜你喜欢
    • 2019-10-01
    • 1970-01-01
    • 2017-01-27
    • 2017-05-23
    • 2015-06-28
    • 2020-12-06
    • 2011-06-08
    • 2020-03-22
    • 1970-01-01
    相关资源
    最近更新 更多