3

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?

1
  • You can easily do it by javascript ryt? Did you tried it by using javascript function? Commented Dec 5, 2014 at 9:56

2 Answers 2

3

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})"
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, thats the way. Thank you! Let me add this important feature: to call my callbackFunction with paramaters i added a third parameter "para", in JSON-format: ng-click="confirmBox('beSureQuestion', functionToCallWhenOkClicked, {para1:value1, para2, value2})". Maybe you edit your answer with that?
Yes, this is good addition. In general it's a good idea to use object to pass many parameters and data. It can even be confirmBox({message: 'beSureQuestion', callback: functionToCallWhenOkClicked, params: {}}).
Oh, there is a typo in the extra-parameter: para2,value2 has to be para2:value2 (no commata) - @dfsq please edit
1

As JavaScript is a functional programming language there are actually no differences between variables and functions. A variable is just a simple function returning its value. So to use functions as parameters simply pass the function to the view by adding it to $scope in your angular controller:

$scope.myCallback = function(anyParameter) {
    //do some stuff
    return "some stuff" + anyParameter;
}

$scope.anyFunction = function(value,callback,parameter) {
    //do something with value
    ...
    //do the callback
    callback(parameter);
}

now you can pass this callback function on within your template:

<div ng-click="anyFunction(someValue, myCallback, someParameter)">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.