如何在 angularjs 的下方按钮中应用确认对话框?
<button class="btn btn-sm btn-danger" ng-click="removeUser($index)">Delete</button>
就像这样。
<span><a class="button" onclick="return confirm('Are you sure to delete this record ?')" href="delete/{{ item.id }}">Delete</span>
更新
目前我是这样做的
function removeUser(index) {
var isConfirmed = confirm("Are you sure to delete this record ?");
if(isConfirmed){
vm.users.splice(index, 1);
}else{
return false;
}
};
最佳答案
这是片段,
你的 HTML 应该是怎样的,
<button class="btn btn-sm btn-danger" ng-confirm-click="Are you sure to delete this record ?" confirmed-click="removeUser($index)">Delete</button>
请在您的自定义 angularjs 文件中包含此指令,
app.directive('ngConfirmClick', [
function(){
return {
link: function (scope, element, attr) {
var msg = attr.ngConfirmClick || "Are you sure?";
var clickAction = attr.confirmedClick;
element.bind('click',function (event) {
if ( window.confirm(msg) ) {
scope.$eval(clickAction)
}
});
}
};
}])
你的 Angular 范围基于你上面提到的删除功能,
$scope.removeUser = function(index) {
vm.users.splice(index, 1);
}
关于javascript - angularjs中的确认对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501303/