在确认模态后执行动作(在指令中)



在做一些动作之前,我已经写了一个指令来打开模态作为确认。模态结果函数会触发,但它不能执行ngConfirmClick动作。

app.directive('ngConfirmClick', function( $modal ) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('click', function() {
                var message = attrs.ngConfirmMessage;
                var action = attrs.ngConfirmClick;
                var modalInstance = $modal.open({
                    templateUrl: 'views/modals/confirmClick.html',
                    controller: 'ConfirmClickCtrl',
                    resolve: {
                        message: function() { 
                            return message;
                        }
                    }
                });
                modalInstance.result.then(function() {
                   scope.$apply(action);
                });
            });
        }
    }
});

<button ng-confirm-message="Really delete user?" ng-confirm-click="delete(user)" type="button" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-remove"></span></button>

知道为什么apply函数不起作用吗?

attribute from markup总是被解释为字符串——这里没有什么特别的。

将回调函数传递给指令的最简单方法如下:

app.directive('ngConfirmClick', function( $modal ) {
    return {
    // ... other options
    scope : {
        action : "&ngConfirmClick" // mind -&- at the beginning
    },
    link : funciton(scope, ...){
         scope.action();
    }
});

_P_L_N_K_R_

最新更新