2015-02-24 86 views
1

我正在使用Angularfire,我想通过复选框删除项目。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”按钮的工作。

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

回答

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

我加checked属性ngModel指令。

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> 
+0

哇,非常感谢。我从来没有想过'检查'属性。它使事情变得简单。 – Doreen 2015-02-24 08:52:30

+0

嗨,我想知道如果我想将按钮checkAll()和按钮'uncheckAll()'组合到一个复选框中,我该怎么办? – Doreen 2015-02-24 09:49:24

+0

我设置了一个简单的例子:http://plnkr.co/edit/m3m5EjQHLMK0IUE3htLU?p=info – dfsq 2015-02-24 10:38:53