Angularfire:删除项目(S) [英] Angularfire: Delete item(s)
问题描述
好了,所以我有一个angularfire应用程序发送文本项火力点,但我无法写的功能,从火力删除它们。
这是我的HTML
<按钮类=BTN BTN-二级NG点击=deleteAll()>删除所有与LT; /按钮> < UL类=消息>
<李NG重复=项目列表中的阶级=项目小组>
< H3> {{item.name}}< / H3 GT&;
&所述p为H.; {{item.message}}&下; / P>
<按钮类=BTNNG点击=deleteThis()>删除< /按钮>
< /李>
< / UL>
deleteThis意指删除单个项目的按钮连接到并deleteAll意指删除所有项目
VAR对myApp = angular.module(对myApp,[火力点]);myApp.controller(SampleCtrl功能($范围,$ firebaseArray){ VAR名单= $ firebaseArray(新火力地堡(https://writeup.firebaseio.com/)); $ scope.list =清单; $ scope.submit =功能(){
变种名称=的document.getElementById(名)。价值,
。消息=的document.getElementById(信息)值; 。名单$添加({名:姓名,邮件:消息}),然后(功能(REF){。
变种的id = ref.key();
。名单$ indexFor(ID);
});
} $ scope.deleteAll =功能(){
。$ scope.id $删除();
}; $ scope.deleteThis =功能(ID,名称,消息){
。$ scope.list $删除(ID);
}});
在你的HTML传递项目到 deleteThis
函数作为参数。</ P>
&LT; UL类=消息&GT;
&LT;李NG重复=项目列表中的阶级=项目小组&GT;
&LT; H3&GT; {{item.name}}&LT; / H3 GT&;
&所述p为H.; {{item.message}}&下; / P&GT;
&LT;按钮类=BTNNG点击=deleteThis(项目)&GT;删除&LT; /按钮&GT;
&LT; /李&GT;
&LT; / UL&GT;
在你的控制器使用的参数。
$ scope.deleteThis =功能(项目){
。$ scope.list $删除(项目);
};
从文档:
$删除(recordOrIndex)
从数据库中,并从本地阵列删除的记录。此方法返回的记录是在服务器上删除后解决的承诺。它将包含一个
火力地堡
引用删除的记录。它可以接受的阵列的索引,或存在的阵列中的一个项目的引用。
块引用>- <一href=\"https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-removerecordorindex\"相对=nofollow> AngularFire API参考 - $删除
Okay, so I've got an angularfire app that sends text entries to firebase, but I'm having trouble writing the functions to delete them from firebase.
here is my HTML
<button class="btn btn-secondary" ng-click="deleteAll()">Remove All</button> <ul class="messages"> <li ng-repeat="item in list" class="item panel"> <h3>{{item.name}}</h3> <p>{{item.message}}</p> <button class="btn" ng-click="deleteThis()">Delete</button> </li> </ul>
deleteThis is meant to delete the single item the button is attached to and deleteAll is meant to delete all items.
var myApp = angular.module("myApp",["firebase"]); myApp.controller("SampleCtrl", function($scope, $firebaseArray) { var list = $firebaseArray(new Firebase("https://writeup.firebaseio.com/")); $scope.list = list; $scope.submit = function(){ var name = document.getElementById("name").value, message = document.getElementById("message").value; list.$add({ name: name, message: message }).then(function(ref) { var id = ref.key(); list.$indexFor(id); }); } $scope.deleteAll = function(){ $scope.id.$remove(); }; $scope.deleteThis = function(id, name, message){ $scope.list.$remove(id); } });
解决方案In your HTML pass the item to the
deleteThis
function as an argument.<ul class="messages"> <li ng-repeat="item in list" class="item panel"> <h3>{{item.name}}</h3> <p>{{item.message}}</p> <button class="btn" ng-click="deleteThis(item)">Delete</button> </li> </ul>
In your controller use the argument.
$scope.deleteThis = function(item){ $scope.list.$remove(item); };
From the Docs:
$remove(recordOrIndex)
Remove a record from the database and from the local array. This method returns a promise that resolves after the record is deleted at the server. It will contain a
Firebase
reference to the deleted record. It accepts either an array index or a reference to an item that exists in the array.-- AngularFire API Reference - $remove
这篇关于Angularfire:删除项目(S)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!