AngularJS ng-click: how to call a function with a function as parameter?

Marcel Ennix

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?

dfsq

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call a service function in AngularJS ng-click (or ng-change, ...)?

From Dev

ng-click gives "href undefined " error on a function call with a parameter

From Dev

Angularjs passing parameter values in ng-click function

From Dev

How to call a function on click

From Dev

How to call a function with a function as parameter?

From Dev

How to call function in ng-click which is defined in factory service

From Dev

how to call form function in ng-click with href text?

From Dev

How to call function in ng-click which is defined in factory service

From Dev

How to bind ng-click to a custom directive and call a parent function?

From Dev

How to add a html button with angularjs with a working ng-click function

From Dev

How to pass an AngularJs ng-click function on a button?

From Dev

Function parameter the same in button ng-click

From Dev

Dynamic ng-click does not call function

From Dev

Angular ng-click with call to a function not working

From Dev

Angularjs - ng-click function vs directive

From Dev

AngularJS ng-click not calling function on scope

From Dev

AngularJS ng-click and callback function

From Dev

AngularJS ng-click not calling function on scope

From Dev

AngularJS ng-click and callback function

From Dev

AngularJS - function not working on ng-click

From Dev

AngularJS ng-click function with Angular Expression parameter returning a Syntax Error

From Dev

Angularjs ng-required call function

From Dev

AngularJS call function of object in ng-repeat

From Dev

angularJS how to $eval the function with parameter

From Dev

angularJs ng-click is not working inside the ng-if function

From Dev

how to call bold function in angularjs?

From Dev

How to call function in controller in angularjs?

From Dev

How to call function in angularjs factory

From Dev

How to call a function in another in angularjs

Related Related

  1. 1

    How to call a service function in AngularJS ng-click (or ng-change, ...)?

  2. 2

    ng-click gives "href undefined " error on a function call with a parameter

  3. 3

    Angularjs passing parameter values in ng-click function

  4. 4

    How to call a function on click

  5. 5

    How to call a function with a function as parameter?

  6. 6

    How to call function in ng-click which is defined in factory service

  7. 7

    how to call form function in ng-click with href text?

  8. 8

    How to call function in ng-click which is defined in factory service

  9. 9

    How to bind ng-click to a custom directive and call a parent function?

  10. 10

    How to add a html button with angularjs with a working ng-click function

  11. 11

    How to pass an AngularJs ng-click function on a button?

  12. 12

    Function parameter the same in button ng-click

  13. 13

    Dynamic ng-click does not call function

  14. 14

    Angular ng-click with call to a function not working

  15. 15

    Angularjs - ng-click function vs directive

  16. 16

    AngularJS ng-click not calling function on scope

  17. 17

    AngularJS ng-click and callback function

  18. 18

    AngularJS ng-click not calling function on scope

  19. 19

    AngularJS ng-click and callback function

  20. 20

    AngularJS - function not working on ng-click

  21. 21

    AngularJS ng-click function with Angular Expression parameter returning a Syntax Error

  22. 22

    Angularjs ng-required call function

  23. 23

    AngularJS call function of object in ng-repeat

  24. 24

    angularJS how to $eval the function with parameter

  25. 25

    angularJs ng-click is not working inside the ng-if function

  26. 26

    how to call bold function in angularjs?

  27. 27

    How to call function in controller in angularjs?

  28. 28

    How to call function in angularjs factory

  29. 29

    How to call a function in another in angularjs

HotTag

Archive