我追加在我的指令中使用bootbox.confirm,但执行错误

时间:2017-08-29 08:11:13

标签: javascript angularjs

HTML:

<a href="#!uploadstatus"><button type="submit" class="btn btn-primary" confirm-ng-click="Are you sure">Submit</button></a>

angular js是:

omapp.directive("confirmNgClick",function(){
    return {
        priority:-1,
        restrict:'A',
        link: function(scope,element,attrs){
        element.bind('click',function(event){

        var message =  attrs.confirmNgClick || "Are you sure?";
        if (message && !confirm(message)){
             event.stopImmediatePropagation();
             event.preventDefault();
        } //if
        }) // bind
        }//function
    }
})

当我点击按钮时,它会发出一个确认框。当我点击“是”时,页面转到下一页。一切都好。我想让确认框变得漂亮,所以我正在使用bootbox。

我已经更改了angularjs指令,如下所示:

omapp.directive("confirmNgClick",function(){
    return {
        priority:-1,
        restrict:'A',
        link: function(scope,element,attrs){
        element.bind('click',function(event){
        var message =  attrs.confirmNgClick || "Are you sure?";
        bootbox.confirm(message,function(result){
            if (! result){
                event.stopImmediatePropagation();
                event.preventDefault();
            }

        })

        }) // bind
        }//function
    }
})

出错了。我该怎么办?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

omapp.directive("confirmNgClick",function(httpPostFactory,$ngBootbox,$location){
    return {
        priority:-1,
        restrict:'A',
        link: function(scope,element,attrs){
        element.bind('click',function(event){
        var message =  attrs.confirmNgClick || "Are you sure?";
        event.stopImmediatePropagation();
        event.preventDefault();
        bootbox.confirm(message,function(result,$location){
            if (result){
                window.location.href = '#!/uploadstatus';
            }
        })
        }) // bind
        }//function
    }
})

上面的代码没问题