通过 angularfire 只删除一个项目 [英] remove just a item by angularfire

查看:20
本文介绍了通过 angularfire 只删除一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想删除一个项目,但我的代码删除了所有项目.我不明白,也找不到其他解决方案,

请帮忙

<input type="text" ng-model="data.name" ng-change="datas.$save(data)"/><input type="text" ng-model="data.age" ng-change="datas.$save(data)"/><a ng-click="removeItem(id)">删除</a>

ref = new Firebase(FIREBASE_URL);authData = ref.getAuth();obj = $firebaseObject(ref);obj.$remove(id);

解决方案

如果 FIREBASE_URL 指向您的列表(甚至整个数据库),那么您的代码确实会删除该列表.这是预期的行为.由于您使用的是 $firebaseObject,因此调用 $remove() 将删除该对象;在本例中为整个项目列表.

仅删除带有 id 的项目:

var ref = new Firebase(FIREBASE_URL);var items = $firebaseArray(ref);items.$remove(items.$indexFor(id));

顺便说一句,这有点令人费解,你可以很容易地做到:

new Firebase(FIREBASE_URL).child(id).remove();

之前已经问过这个问题,所以请对搜索友好:

I just want to remove an item, but my code removes all the items. I don't understand and can't find another solution,

please help

<div ng-repeat="(id, data) in datas">
<input type="text" ng-model="data.name" ng-change="datas.$save(data)" />  
<input type="text" ng-model="data.age" ng-change="datas.$save(data)" />
<a ng-click="removeItem(id)">delete</a>
</div>


ref = new Firebase(FIREBASE_URL);
authData = ref.getAuth();
obj = $firebaseObject(ref);    
obj.$remove(id);

解决方案

If FIREBASE_URL points to your list (or even entire database), then indeed your code will delete that list. This is expected behavior. Since you're using $firebaseObject, calling $remove() on that will remove the object; in this case the entire list of items.

To remove only the item with id:

var ref = new Firebase(FIREBASE_URL);
var items = $firebaseArray(ref);    
items.$remove(items.$indexFor(id));

This is a bit convoluted btw, you can just as easily do:

new Firebase(FIREBASE_URL).child(id).remove();

This has been asked before, so please become friendly with search:

这篇关于通过 angularfire 只删除一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆