我想在存储中添加一个特别的收藏夹,我可以在存储中添加我想要的收藏夹的数量,但是我最喜欢的只有1个! 有image可以凸显您的思想!
我希望能够存储多达一个收藏夹(由房子确认),而其他收藏夹则是普通收藏夹(心)。我向您展示一些代码,并对其进行更好的解释。
search.page.ts
getItems(ev) {
this.loadFavorites();
var val = ev.target.value;
if (val && val.trim() != '') {
this.storageService.getFavorites().then(favorites => {
this.favorites = this.favorites.filter((favorite) => {
return (favorite.adresse.toLowerCase().indexOf(val.toLowerCase()) > -1);
});
});
}
}
@ViewChild('mylist') mylist: IonList;
// CREATE
addFavorite(adresse: string, lat: string, lon: string) {
this.newFavorite.adresse = adresse.replace(/[, ]+/g, " ").trim()
this.newFavorite.lat = lat
this.newFavorite.lon = lon
this.newFavorite.modified = false;
this.newFavorite.id = Date.now();
this.storageService.addFavorite(this.newFavorite).then(favorite => {
this.newFavorite = <Favorite>{};
console.log(this.newFavorite.adresse)
this.showToast('Favorite added!');
this.loadFavorites()
});
}
addFavoriteHome(adresse: string, lat: string, lon: string) {
this.newFavorite.adresse = adresse.replace(/[, ]+/g, " ").trim()
this.newFavorite.lat = lat
this.newFavorite.lon = lon
this.newFavorite.modified = true;
this.newFavorite.id = Date.now();
this.storageService.addFavorite(this.newFavorite).then(favorite => {
this.newFavorite = <Favorite>{};
this.showToast('Favorite added!')
this.loadFavorites()
});
}
您可以在此代码上看到,当我添加一个新的收藏夹房屋时,它会添加this.newFavorite.modified = true;
,对于普通收藏夹它是false
。
我可以轻松添加和删除它,但是我想设置一个限制,我只希望一个对象可以拥有this.newFavorite.modified = true;
。
search.page.html
<ion-list mode="md" #mylist>
<div *ngFor="let favorite of favorites">
<ion-item mode="md">
<ion-label text-wrap>
<p>{{favorite.modified}}</p>
<h3 routerLink="/home/{{favorite.adresse}}">{{ favorite.adresse | slice:0:30 }}...</h3>
</ion-label>
<img *ngIf="favorite.modified" (click)="addFavoriteHome(favorite)" src="../../assets/icon/home-full.svg" style="width: 35px;" alt="">
<img *ngIf="!favorite.modified" (click)="addFavorite(favorite)" src="../../assets/icon/home.svg" style="width: 35px;" alt="">
<ion-icon name="heart" (click)="deleteAlert(favorite)" end></ion-icon>
</ion-item>
</div>
</ion-list>
答案 0 :(得分:0)
我做了一个forEach方法,将所有喜欢的1乘1更新,并且有效!
this.favorites.forEach((favorites, index) => {
console.log(favorites)
if (favorites.modified == true) {
favorites.modified = false
this.storageService.updateItem(favorites)
this.storageService.getFavorites().then(favorites => {
this.favorites = favorites;
});
}
})