I want to code a confirmation-box-function (opens a modal), called within a ng-click directive.
Inside this confirmation-modal i have two buttons: Cancel + Ok. A click on Cancel dismisses the modal. A click on Ok has to call the (callback)-function given through my ng-click-call (with parameters):
<button ng-click="confirmBox('beSureQuestion', functionToCallWhenOkClicked(parameter))">Delete</button>
Is there a way to give a function as parameter inside the ng-click?
I dont want to use if-statements inside my html-template.
Any suggestions?
No, you can do it the way you are trying to do. The problem is that ngClick will execute two functions functionToCallWhenOkClicked(parameter)
and confirmBox
at the same time because you invoke them immediately with ()
.
What you can do however is to pass a function reference like this:
<button ng-click="confirmBox('beSureQuestion', functionToCallWhenOkClicked)">Delete</button>
and them call it from controller when needed:
$scope.confirmBox = function(msg, okCallback) {
// some checks ...
okCallback();
}
UPD. From the comment by @Marcel Ennix. To be able to pass additional parameters it's optimal to pass one more argument to confirmBox
function:
ng-click="confirmBox('beSureQuestion', functionToCallWhenOkClicked, {para1:value1, para2, value2})"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments