Angularjs 角火-移除单个项目
以下是我认为相关的代码:Angularjs 角火-移除单个项目,angularjs,pug,firebase,angularfire,Angularjs,Pug,Firebase,Angularfire,以下是我认为相关的代码: p(ng-repeat="t in todos") input( type="checkbox", ng-model="t.done", ng-click="clearItem($event)" ) {{t.text}} done? {{t.done}} 单击复选框时,我希望从数据库中删除todos数组中的相应对象 我的clearItem功能如下: $scope.clearItem = function(event) {
p(ng-repeat="t in todos")
input(
type="checkbox",
ng-model="t.done",
ng-click="clearItem($event)"
)
{{t.text}} done? {{t.done}}
单击复选框时,我希望从数据库中删除todos
数组中的相应对象
我的clearItem
功能如下:
$scope.clearItem = function(event) {
todoRef.remove($scope.t);
}
但是,这会删除数据库中的所有条目。我希望它只删除有问题的特定对象。还有什么我可以做的吗?好的,我想好了
使用
ng repeat
循环时,请在TODO中使用(id,t)
。这允许您将id
作为参数发送到ng click
函数,并且$scope.todos.$remove(id)
工作正常。更好的解决方案是使用$scope.clearItem()
将对象t
作为参数,而不是$event
HTML-
JS-
$scope.clearItem=函数(obj){todoRef.$remove(obj)}代码>我能够删除该项目的唯一方法是使用从firebase获得的阵列上的循环
var ref= new Firebase('https://Yourapp.firebaseio.com/YourObjectName');
var arr_ref=$firebaseArray(ref);
for(var i=0;i<arr_ref.length;i++){
if(key==arr_ref[i].$id){
console.log(arr_ref[i]);
arr_ref.$remove(i);
}
}
var ref=new Firebase('https://Yourapp.firebaseio.com/YourObjectName');
var arr_ref=$firebaseArray(ref);
对于(var i=0;i来说,移除对象的最简单方法是
scope.clearItem = function(event) {
todoRef.$loaded().then(function(){
todoRef.$remove($scope.t)
});
野兽的异步特性让我有过几次机会。为了给其他在这里着陆的人提供一个更完整的例子,根据这一点,这将是首选的方法,我相信移除对象最简单的方法是:
// Create an app. Synch a Firebase array inside a controller
var myApp = angular.module("myApp", ["firebase"]);
// inject $firebaseArray
myApp.controller("TodoCtrl", ["$scope", "$firebaseArray", function($scope, $firebaseArray) {
// bind $scope.todos to Firebase database
$scope.todos = $firebaseArray(myFirebaseRef.child("todo"));
// create a destroy function
$scope.removeTodo = function(todo) {
$scope.todos.$remove(todo);
};
}]);
在您看来,您可以执行以下操作。请注意,您可以将removeTodo函数绑定到问题指定的复选框或常规的旧
希望这能有所帮助!slfan现在就来看看这个
// In your view
<div ng-controller="TodoCtrl">
<ul>
<li ng-repeat="todo in todos">
{{ todo.text }} : <a href ng-click="removeTodo(todo)">X</a>
</li>
</ul>
</div>