Angularfire按复选框删除项目

时间:2015-02-24 07:40:05

标签: angularjs checkbox firebase angularfire removeall

我正在使用Angularfire,我想通过复选框删除这些项目。

还有一个按钮可以执行“checkAll”而另一个按钮可以执行“uncheckAll”

HTML

<div ng-app="app" ng-controller="Ctrl">
  <li ng-repeat="item in newslist">
    <input type="checkbox"> {{item.newsTitle}}
  </li>
  <br>
  <button ng-click="checkAll()">check all</button>
  <button ng-click="uncheckAll()">uncheck all</button>
  <button ng-click="newslist.$remove(item)">Remove</button>
</div>

JS

var app = angular.module("app", ["firebase"]);
app.value('newsURL', 'https://XXX.firebaseio.com/XXX/');
app.factory('newsFactory', function($firebase, newsURL) {
  return $firebase(new Firebase(newsURL)).$asArray();
});
app.controller('Ctrl', function($scope, $firebase, newsFactory) {
  $scope.newslist = newsFactory;
  $scope.checkAll = function() {

  };
  $scope.uncheckAll = function() {

  };
});

plunker here

我不知道如何通过复选框删除项目或如何使“checkAll”按钮工作。

如果有人能告诉我你的答案,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

以下是检查/取消选中和删除项目所需的功能:

$scope.checkAll = function() {
    $scope.newslist.forEach(function(el) {
        el.checked = true;
        $scope.newslist.$save(el);
    });
};

$scope.uncheckAll = function() {
    $scope.newslist.forEach(function(el) {
        el.checked = false;
        $scope.newslist.$save(el);
    });
}

$scope.remove = function() {
    $scope.newslist.forEach(function(item) {
        if (item.checked) {
            $scope.newslist.$remove(item);
        }
    });
};

演示: http://plnkr.co/edit/GsGVsGGjNwW4i1kTOuko?p=preview

我为ngModel指令添加了checked属性。

HTML变为:

<li ng-repeat="item in newslist">
    <input type="checkbox" ng-model="item.checked">{{item.newsTitle}}
</li>

<button ng-click="checkAll()">check all</button>
<button ng-click="uncheckAll()">uncheck all</button>
<button ng-click="remove()">Remove</button>