从localstorage中删除后不会自动删除

时间:2016-02-24 14:00:19

标签: angularjs html5 ionic-framework local-storage angular-local-storage

首先,这是代码

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;
        }])

favorites.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中的索引被成功删除后,favorites.html上的数据无法更新,无法从中删除立即获得favorties.html。

我需要先刷新浏览器,根据本地存储显示最新列表。

我希望能够在不刷新页面的情况下显示基于本地存储数据的最新数据。

我在这里做错了吗?

提前谢谢!

0 个答案:

没有答案