删除功能不适用于angularJS
问题描述:
我的js代码:
camListApp.controller("Hello", function($scope, $http, $uibModal){
$scope.del=function(data){
var result=confirm('are you sure?');
if(result==true){
var index=getSelectedIndex(data);
$scope.records.splice(index, 1);
}
};
function getSelectedIndex(data) {
for (var i =0; i<$scope.records.length; i++)
if($scope.records[i].data==data)
return i;
return -1;
}
HTML代码:
<td><button class="btn" ng-click="del(record.filename)">Delete</button></td>
我的json数据:
[{"cameraid":"000000001","timestamp":"2016-07-09 10:06","filename":"c037731fc2256177ba29c7893caacf04","locationid":"Bedok01"}
{"cameraid":"000000003","timestamp":"2016-07-13 11:35","filename":"4fd2413d30073b4b6a5cacbb8b7c1965","locationid":"Bedok01"}
{"cameraid":"000000003","timestamp":"2016-07-13 14:41","filename":"6b6b62948eb679efeb650d609c85b7aa","locationid":"Bedok01"}
我该如何在angularjs上执行删除功能,并单击该按钮,同时mongodb也会删除数据.有人可以帮忙吗?
How can i do a delete function on angularjs and when the button is clicked and at the same time mongodb also remove the data. Anybody can help?
答
进行http调用以从数据库中删除,然后使用如下所示的splice从列表中删除对象
make http call to delete from db and then remove the object from list using splice as below
<td><button class="btn" ng-click="del(record)">Delete</button></td>
$scope.del = function(record) {
var index = $scope.records.indexOf(record);
$scope.records.splice(index , 1);
$scope.list();
};