george
george

Reputation: 97

angularjs: confirmation dialog not working properly

i am trying to create a confirmation dialog in angularjs, i found an answered question about that in stackoverflow Confirmation dialog on ng-click - AngularJS i tried it as it is answered but when i click cancel it works as if i clicked OK is the answer wrong or should i add something else

    <button type="button"  ng-click="updateData()" ng-confirm-click="Are you sure?">
            Update
    </button>

directive

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

Upvotes: 2

Views: 3434

Answers (2)

Viplock
Viplock

Reputation: 3319

The answer is write. Just Change "ng-click" to "confirmed-click" and it will work fine

<button type="button"  confirmed-click="updateData()" ng-confirm-click="Are you sure?">
            Update
    </button>

Just check plank Here

Upvotes: 0

MarcoS
MarcoS

Reputation: 17711

I had the same problem, and I solved using your same directive, but, instead of using ng-click for the final action on user confirmation, I did use the attribute confirmed-click with the final action as it's value.

html:

<button confirmed-click="confirm()" ng-confirm-click="Are you really sure?">Update</button>

directive:

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

See this fiddle.

Upvotes: 2

Related Questions