3

is there a way to toggle the ng-click value on a button, but when the value is a function?

use something similar to toggle classes : ng-class:"funtionToggle?'disabled':'enabled'"

but when the value is a function: ng-click="funnyFunction()"

I want to execute funnyFunction() if the user select certain options in a form, and execute seriousFunction() if the user select different ones, but I need to use the same button, just change the function that will be called depending on the user interaction.

1
  • Question is confusing as to exactly what you are trying to do. What does "change the function that will be called depending on the user interaction" mean? Commented Oct 12, 2017 at 23:19

2 Answers 2

1

yes you can, but not like you want to do it. Don't write your logic in html, write it in javascript, like this:

<button ng-click="someFunction()">Click here</button>

and in javascript:

$scope.someFunction() {
  if($scope.form.someOption) {
    funnyFunction();
  } else {
    seriousFunction();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use ng-click="this[method]()"

  <form action="">
    <input ng-click="sendFunctionName(choice.select)" ng-model="choice.select" type="radio" name="gender" value="function1"> Function1<br>
    <input ng-click="sendFunctionName(choice.select)" ng-model="choice.select" type="radio" name="gender" value="function2"> Function2<br> 
  </form>

from the above form, the user will choose an option, based on that 'Button' value will be changed which is a 'function name' here.

  <button ng-click="this[choice.select]()">
    Button 
  </button>

Working Jsfiddle here: http://jsfiddle.net/mkarrfan/o89yguk9/

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.