0

まず、コードはこちら

controller.js

.controller('FavoritesController', ['$scope', 'dishes', 'favorites', 'favoriteFactory', 'baseURL', '$ionicListDelegate', '$ionicPopup', '$ionicLoading', '$timeout', '$localStorage', function ($scope, dishes, favorites, favoriteFactory, baseURL, $ionicListDelegate, $ionicPopup, $ionicLoading, $timeout, $localStorage) {

        $scope.baseURL = baseURL;
        $scope.shouldShowDelete = false;

        $scope.favorites = favorites;

        $scope.dishes = dishes;

        console.log($scope.dishes, $scope.favorites);

        $scope.toggleDelete = function () {
            $scope.shouldShowDelete = !$scope.shouldShowDelete;
            console.log($scope.shouldShowDelete);
        }

        $scope.deleteFavorite = function (index) {

            var confirmPopup = $ionicPopup.confirm({
                title: 'Confirm Delete',
                template: 'Are you sure you want to delete this item?'
            });

            confirmPopup.then(function (res) {
                if (res) {
                    console.log('Ok to delete');
                    favoriteFactory.deleteFromFavorites(index);

                    var old_favorite = JSON.parse($localStorage.get('favorites'));
                    var leng = Object.keys(old_favorite).length;

                    for (var i = 0; i < leng; i++) {
                        if (Object.keys(old_favorite)[i] == index) {
                            console.log("Deleted from localstorage! " + Object.keys(old_favorite)[i]);
                            old_favorite.splice(old_favorite.indexOf(index), 1);
                            $localStorage.storeObject('favorites', old_favorite);

                        }
                    }


                } else {
                    console.log('Canceled delete');
                }
            });

            $scope.shouldShowDelete = false;

        }
    }])

services.js

.factory('favoriteFactory', ['$resource', 'baseURL', function ($resource, baseURL) {
        var favFac = {};
        var favorites = [];

        favFac.addToFavorites = function (index) {
            for (var i = 0; i < favorites.length; i++) {
                if (favorites[i].id == index)
                    return;
            }
            favorites.push({id: index});
        };

        favFac.deleteFromFavorites = function (index) {
            for (var i = 0; i < favorites.length; i++) {
                if (favorites[i].id == index) {
                    favorites.splice(i, 1);
                    console.log("Deleted !" + index);
                }
            }
        }

        favFac.getFavorites = function () {
            return favorites;
        };

        return favFac;
        }])

お気に入り.html

<ion-view view-title="My Favorites">
    <ion-nav-buttons side="secondary">
      <div class="buttons">
        <button class="button button-icon icon ion-ios-minus-outline"
          ng-click="toggleDelete()"></button>
      </div>
    </ion-nav-buttons>
  <ion-content>
    <ion-list show-delete="shouldShowDelete">
      <ion-item ng-repeat="dish in dishes | favoriteFilter:favorites" href="#/app/menu/{{dish.id}}" class="item-thumbnail-left" on-swipe-left="deleteFavorite(dish.id)">
        <img ng-src="{{baseURL+dish.image}}" on-swipe-left="deleteFavorite(dish.id)">
        <h2>{{dish.name}}
         <span style="font-size:75%">{{dish.price | currency}}</span>
        <span class="badge badge-assertive">{{dish.label}}</span></h2>
        <p>{{dish.description}}</p>
        <ion-delete-button class="ion-minus-circled"
          ng-click="deleteFavorite(dish.id)">
        </ion-delete-button>
      </ion-item>
    </ion-list>
  </ion-content>
 </ion-view>

app.js

 .state('app.favorites', {
  url: '/favorites',
  views: {
    'mainContent': {
      templateUrl: 'templates/favorites.html',
        controller:'FavoritesController',
      resolve: {
          dishes:  ['menuFactory', function(menuFactory){
            return menuFactory.query();
          }],
                        favorites: ['favoriteFactory', function(favoriteFactory) {
              return favoriteFactory.getFavorites();
          }]
      }
    }
  }
})

それで、controller.js から deleteFavorite 関数を使用しようとして、localstorage 内のインデックスが正常に削除された後、favorties.html のデータを更新できず、favorties.html からすぐに削除できませんでした。

localstorage に基づいて最新のリストを表示するには、最初にブラウザを更新する必要があります。

ページを更新せずに localstorage データに基づいて最新のデータを表示できることを願っています。

ここで何か間違ったことをしましたか?

前もって感謝します!

4

0 に答える 0