ng-click 上的确认对话框 - AngularJS [英] Confirmation dialog on ng-click - AngularJS

查看:23
本文介绍了ng-click 上的确认对话框 - AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义 angularjs 指令在 ng-click 上设置确认对话框:

I am trying to setup a confirmation dialog on an ng-click using a custom angularjs directive:

app.directive('ngConfirmClick', [
    function(){
        return {
            priority: 1,
            terminal: true,
            link: function (scope, element, attr) {
                var msg = attr.ngConfirmClick || "Are you sure?";
                var clickAction = attr.ngClick;
                element.bind('click',function (event) {
                    if ( window.confirm(msg) ) {
                        scope.$eval(clickAction)
                    }
                });
            }
        };
}])

这很好用,但不幸的是,使用我的指令的标签内的表达式不会被评估:

This works great but unfortunately, expressions inside the tag using my directive are not evaluated:

<button ng-click="sayHi()" ng-confirm-click="Would you like to say hi?">Say hi to {{ name }}</button>

(在这种情况下不评估名称).这似乎是由于我的指令的终端参数.你有任何解决方法的想法吗?

(name is not evaluated is this case). It seems to be due to the terminal parameter of my directive. Do you have any ideas of workaround?

要测试我的代码:http://plnkr.co/edit/EHmRpfwsgSfEFVMgRLgj?p=preview

推荐答案

如果您不介意不使用 ng-click,它可以正常工作.您可以将其重命名为其他名称并仍然读取该属性,同时避免点击处理程序被触发两次目前存在的问题.

If you don't mind not using ng-click, it works OK. You can just rename it to something else and still read the attribute, while avoiding the click handler being triggered twice problem there is at the moment.

http://plnkr.co/edit/YWr6o2?p=preview

我认为问题在于 terminal 指示其他指令不要运行.{{ }} 的数据绑定只是 ng-bind 指令的别名,大概被 terminal 取消了.

I think the problem is terminal instructs other directives not to run. Data-binding with {{ }} is just an alias for the ng-bind directive, which is presumably cancelled by terminal.

这篇关于ng-click 上的确认对话框 - AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆