yavg
yavg

Reputation: 3051

Execute a function with an ng-click, but the second function, execute it if it meets condition

I am currently validating a form. I have a function called "myFunction".

<button class="btn btn-lg btn-primary btn-block" type="submit" ng-click='form.$valid && myFunction()'>SEND</button>

I want the function to run when is clicked, if the form is valid, ie if

Form.$valid == true.

the function "myFunction()" is executed.

I have other function, called "directive_function()". I need execute always this function, when is clicked this button, but the second function "directive_function()" only if the condition is is fulfilled.

I need to do this directly in the html view, I should not and do not want to put anything in the controller. How can I do it?

more or less I need:

ng-click='directive_function(); form.$valid && myFunction()'

http://jsfiddle.net/4973nesL/

Upvotes: 0

Views: 64

Answers (1)

AJ Funk
AJ Funk

Reputation: 3187

You shouldn't be doing this from your view. You should restructure this in order to move the logic into your controller. You can do something like:

<button ng-click='directive_function();'>SEND</button>

and in your controller

directive_function() {
  if(...) {
    // only execute function if your condition is met
    myFunction();
  }
}

Upvotes: 2

Related Questions