我有一个自定义指令来确认点击元素的用户是否真的想要执行操作:
.directive('ngReallyClick', ['$modal',
function ($modal) {
return {
restrict: 'A',
scope: {
ngReallyClick: "&",
},
link: function (scope, element, attrs) {
var isDeleteDisabled = scope.ngReallyDisabled;
element.bind('click', function () {
if (isDeleteDisabled != true) {
var message = attrs.ngReallyMessage || "Are you sure ?";
...
var modalInstance = $modal.open({
template: modalHtml,
controller: ModalInstanceCtrl
});
modalInstance.result.then(function () {
scope.ngReallyClick({ ngReallyItem: scope.ngReallyItem }); //raise an error : $digest already in progress
}, function () {
//Modal dismissed
return false;
});
};
});
}
}
}]);
用于例如:
<a ng-really-message="Are you sure you want to save and close?" ng-really-click="saveAndCloseGlobal(456)"
当用户确认他们的选择时调用saveAndCloseGlobal
。但是,如果我尝试将$event
传递给此函数,以获取原始click
事件,则最终未定义。如果我使用普通ng-click=saveAndCloseGlobal($event)
,那么我会在saveAndCloseGlobal
中获得正确的事件对象。