我在使用PHP和angular.js进行删除操作时遇到问题。在这里我需要在用户删除之前显示警报提示。在此警报提示中,如果我将点击确定按钮,用户将删除,如果我将点击取消按钮,它将不会删除任何内容。我已经做了一些编码,我在下面解释
</div>?
rolecontroller.js:
<a ui-sref='dashboard.role'>
<input type='button' class='btn btn-xs btn-red' value='Delete' ng-click="deleteRoleData(role.id);" ng-confirm-click="Are you sure to delete this user role ?" >
</a>
这里我的问题是,当用户点击取消按钮时,数据也将从DB中删除,这不应该发生。请帮我解决这个问题。
答案 0 :(得分:0)
您应该将控制器添加到指令并像这样使用它:
HTML:
<input type='button' class='btn btn-xs btn-red' value='Delete' role="role.id" ng-confirm-click="Are you sure to delete this user role ?">
JS:
var app = angular.module("app", []);
app.directive('ngConfirmClick', [
function () {
return {
controller: function ($scope, $http) {
$scope.deleteRoleData = function (rid) {
id = rid;
var userdata = {
'userid': id
};
$http({
method: 'POST',
url: "php/userrole/deleteRoleData.php",
data: userdata,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function successCallback(response) {
//console.log('delete',response);
alert(response.data);
}, function errorCallback(response) {
alert(response.data);
});
}
},
link: function (scope, element, attr) {
var msg = attr.ngConfirmClick || "Are you sure?";
var role = attr.role;
attr.ngClick = "";
element.bind('click', function (event) {
if (window.confirm(msg)) {
scope.deleteRoleData(role);
}
});
}
};
}]);
PS我仅为JSFiddle
删除了$state