在NG禁用推送之前Angularjs检查数组

时间:2015-10-12 13:02:39

标签: javascript jquery angularjs ng-storage

每件衣服都有一个ID,衣服ID可以在$StateParams功能和页面URL中找到 - 例如#/ dresses / 21.

当用户点击Heart(Like)或Broken Heart(Dislike)按钮时,衣服ID放在localstorage Heart / Broken Heart阵列中 - 见下文。

此外,当单击任一按钮时,该按钮会自行禁用,从而防止多个衣服ID被按下!单击opersit按钮可以恢复此功能。

问题!
我无法弄清楚如何防止在刷新页面后提交相同的衣服ID - 我在推送衣服ID之前尝试搜索阵列,但这并没有起作用。此外,我可以通过防止双击来防止提交双礼服ID,但是如果我刷新页面按钮能够将礼服ID推送到阵列,尽管它已经有礼服ID出现!

任何指导都会有所帮助! 谢谢。

设计:
enter image description here
HTML:

<ul class="railcontrols">
  <li>
    <md-button aria-label="" class="md-fab md-mini" ng-click="heart()" ng-dblclick="return false;" ng-disabled="heartflag">
      <md-icon md-svg-src="svg/heart.svg"></md-icon>
    </md-button>
  </li>
  <li>
    <md-button aria-label="" class="md-fab md-mini" ng-click="brokenheart()" ng-dblclick="return false;" ng-disabled="brokenheartflag">
      <md-icon md-svg-src="svg/brokenheart.svg"></md-icon>
    </md-button>
  </li>
</ul>

控制器:

   fittingApp.controller('railsCtrl', ['$scope', '$localStorage', '$stateParams','$filter', function($scope, $localStorage, $stateParams, $filter) {
      $scope.$storage = $localStorage;
      var dressID = $stateParams.id;
      if ($scope.$storage.userLoveList === undefined) {
        var getUserID = $scope.$storage.profileData.userID;
        $scope.$storage.userLoveList = ({
          'userID': getUserID,
          'heart': [],
          'brokenheart': []
        });
      }
      $scope.heartflag = false;
      $scope.heart = function() {
        $scope.heartflag = true;
        $scope.brokenheartflag = false;
        // Push Heart
        $scope.$storage.userLoveList.heart.push({'dressID': dressID});
        // Remove BrokenHeart
        $scope.$storage.userLoveList.brokenheart = $filter('filter')($scope.$storage.userLoveList.brokenheart, {dressID: '!'+dressID});
      };
      $scope.brokenheartflag = false;
      $scope.brokenheart = function() {
        $scope.brokenheartflag = true;
        $scope.heartflag = false;
        // Push Broken Heart
        $scope.$storage.userLoveList.brokenheart.push({'dressID': dressID});
        // Remove Heart
        $scope.$storage.userLoveList.heart = $filter('filter')($scope.$storage.userLoveList.heart, {dressID: '!'+dressID});
      };
    }]);

OUTPUT {{$ storage | JSON}}

{
  "userLoveList": {
    "userID": "1",
    "heart": [
      {
        "dressID": "21",
        "dressID": "21", // Prevent Duplicates
        "dressID": "19"
      }
    ],
    "brokenheart": [
     {
        "dressID": "10"
     }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

更改您的代码:

fittingApp.controller('railsCtrl', ['$scope', '$localStorage', '$stateParams', function($scope, $localStorage, $stateParams) {
    $scope.$storage = $localStorage;
    var dressID = $stateParams.id;
    if ($scope.$storage.userLoveList === undefined) {
        var getUserID = $scope.$storage.profileData.userID;
        $scope.$storage.userLoveList = ({
            'userID': getUserID,
            'heart': [],
            'brokenheart': []
        });
    }

    function remove(list, value) {
        var index = list.indexOf(value);
        if (index > -1) {
            list.splice(index, 1);
        }
    }

    $scope.heartflag = $scope.$storage.userLoveList.heart.indexOf(dressID) > -1;
    $scope.brokenheartflag = $scope.$storage.userLoveList.brokenheart.indexOf(dressID) > -1;

    $scope.heart = function() {
        $scope.heartflag = true;
        $scope.brokenheartflag = false;
        // Push Heart
        $scope.$storage.userLoveList.heart.push(dressID);

        // Remove BrokenHeart
        remove($scope.$storage.userLoveList.brokenheart, dressID);
    };

    $scope.brokenheart = function() {
        $scope.heartflag = false;
        $scope.brokenheartflag = true;
        // Push Broken Heart
        $scope.$storage.userLoveList.brokenheart.push(dressID);
        // Remove Heart
        remove($scope.$storage.userLoveList.heart, dressID);
    };
}]);

以下是发生的事情:

我们首先检查dressIDheart的列表中是否已有brokenheart,以便我们可以禁用特定按钮。现在,由于我们已经禁用了特定按钮,因此用户将无法单击两次。