简体   繁体   English

按钮类型为“ submit”的确认对话框指令-AngularJS

[英]Confirmation dialog directive on button type=“submit” - AngularJS

I want to create a directive for confirmation dialog, when I submit the form. 提交表单时,我想为确认对话框创建指令。 The question I found here is similar, but it doesn't solve my issue: 我在这里发现的问题是相似的,但不能解决我的问题:

<button confirm-ng-click="Reset password?" type="submit" class="md-primary">Reset</button>

And the JS: 和JS:

(function () {
    'use strict';

    angular.module('haha', [])
        .directive('confirmNgClick', [ConfirmNgClick]);

    function ConfirmNgClick() {
        return {
            priority: -1,
            restrict: 'A',
            link: function (scope, element, attrs) {
                element.bind('click', function (event) {
                    var message = attrs.confirmNgClick;
                    if (message && !confirm(message)) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                    }
                })
            }
        }
    }
})();

So, when I click button, the dialog doesn't show up. 因此,当我单击按钮时,对话框不会出现。 What am I missing? 我想念什么?

Plugging your code as is into fiddle and looking at the console produced this error: 将您的代码按原样插入小提琴并查看控制台会产生此错误:

Uncaught Error: [$injector:nomod] Module 'haha' is not available! 未捕获的错误:[$ injector:nomod]模块“ haha​​”不可用! You either misspelled the module name or forgot to load it. 您可能拼错了模块名称,或者忘记了加载它。 If registering a module ensure that you specify the dependencies as the second argument. 如果注册模块,请确保将依赖项指定为第二个参数。

You need to specify an array as the second argument when registering a module. 注册模块时,需要将数组指定为第二个参数。

 (function() { 'use strict'; // Notice second argument here angular.module('haha', []) .directive('confirmNgClick', [ConfirmNgClick]); function ConfirmNgClick() { return { priority: -1, restrict: 'A', link: function(scope, element, attrs) { element.bind('click', function(event) { var message = attrs.confirmNgClick; if (message && !confirm(message)) { event.stopImmediatePropagation(); event.preventDefault(); } }) } } } })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="haha"> <button confirm-ng-click="Reset password?" type="submit" class="md-primary">Reset</button> </div> 

I'm just guessing here, but I think your problem is the following 我只是在猜测,但我认为您的问题如下

angular.module('haha')
    .directive('confirmNgClick', [ConfirmNgClick]);

The angular.module function expects (at least) two arguments when you create you module the first time . 首次创建模块时angular.module函数需要(至少)两个参数。 See the related documentation which says that: 请参阅相关文档 ,其中指出:

If specified [the second argument] then new module is being created. 如果指定了[第二个参数],则将创建新模块。 If unspecified then the module is being retrieved for further configuration. 如果未指定,则将检索模块以进行进一步配置。

So change above code to 所以将上面的代码更改为

angular.module('haha', [])
    .directive('confirmNgClick', [ConfirmNgClick]);

Please note the second [] parameter. 请注意第二个[]参数。

See this plnkr for working example. 有关工作示例,请参见此plnkr。

If you remove the second [] parameter then an error is raised on the console by the way. 如果删除第二个[]参数,则在控制台上会引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM