If in ng-click

Is there a way to set a condition inside ng-click? Here, I want the form not to be submitted if there are any form errors, but then I got a parsing exception.

<input ng-click="{{if(profileForm.$valid) updateMyProfile()}}" name="submit" id="submit" value="Save" class="submit" type="submit"> 

I tried to use ng-disabled, but then my validation plugin does not work, so the form never submits at all, therefore it does not start.

+55
javascript angularjs
Feb 14 '14 at 18:07
source share
7 answers

Do not put the condition expression in the template.

Do it on the controller.

Template:

 <input ng-click="check(profileForm.$valid)" name="submit" id="submit" value="Save" class="submit" type="submit"> 

Controller:

 $scope.check = function(value) { if (value) { updateMyProfile(); } } 
+115
Feb 14 '14 at 18:14
source share
— -

It may be useless and useless, but, like javascript, you do not need to use the triple version, as suggested above, in the ng-click statement. You should also be able to use the lazy evaluation syntax ("or die"). So, for your example above:

 <input ng-click="{{if(profileForm.$valid) updateMyProfile()}}" name="submit" id="submit" value="Save" class="submit" type="submit"> 

will become:

 <input ng-click="profileForm.$valid && updateMyProfile()" name="submit" id="submit" value="Save" class="submit" type="submit"> 

In this case, if the profile is invalid, nothing happens, otherwise updateMyProfile () is called. Like the @falinsky link above.

+46
Mar 12 '15 at 2:04
source share

Here is the hack I discovered that might work for you, although its not very pretty, and I'm personally confused to use such a line of code:

 ng-click="profileForm.$valid ? updateMyProfile() : alert('failed')" 

Now you should be thinking: “But I do not want it to warn (“ failed ”) if my profileForm invalid. Well, that is the ugly part. For me, no matter what I put in the else clause of this triple expression, never not executed.

However, if it is deleted, an error occurs. So you can just fill it with a meaningless warning.
I told you that it was ugly ... but I'm not even mistaken when I do something like this.
The right way to do this, as Chen-tzu said, but to each of them.

+25
Feb 27 '14 at 18:59
source share

If you need to do it this way, here are a few ways to do it:

Disabling a button with ng-disabled

Today it is the easiest solution.

 <input ng-disabled="!profileForm.$valid" ng-click="updateMyProfile()" ... > 

Hiding a button (and showing something else) with ng-if

It might be nice if you show / hide some complex markup.

 <div ng-if="profileForm.$valid"> <input ng-click="updateMyProfile()" ... > </div> <div ng-if="!profileForm.$valid"> Sorry! We need all form fields properly filled out to continue. </div> 

(remember that there is no ng-else ...)

Connection of both

Communicating with the user where the button is located (he will no longer search for it), but explain why you can’t click it.

 <input ng-disabled="!profileForm.$valid" ng-click="updateMyProfile()" ... > <div ng-if="!profileForm.$valid"> Sorry! We need all form fields properly filled out to continue. </div> 
+8
Nov 08 '15 at 21:29
source share

We can add the ng-click event conditionally without using a disconnected class.

HTML:

  <input ng-click="profileForm.$valid && updateMyProfile()" name="submit" id="submit" value="Save" class="submit" type="submit"> 
+4
Jul 10 '15 at 12:21
source share

From http://php.quicoto.com/inline-ifelse-statement-ngclick-angularjs/ , so you will do this if you really need to:

 ng-click="variable = (condition=='X' ? 'Y' : 'X')" 
+4
03 Sep '15 at 23:01
source share

You can put conventions inside tags. Try:

 ng-class="{true:'active',false:'disable'}[list_status=='show']" 
0
Sep 10 '15 at 10:02
source share



All Articles