Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable ng-click on certain conditions of application for all types of element

In my application I've binded several elements with ng-click directive like below

<a ng-click="DoSomething()"/> <button ng-click="DoSomethingElse()">xyz</button> <span ng-click="DoSomething()"></span> 

Now in my application in certain scenarios I want default behavior of ng-click to happen, but in certain scenarios I want to completely disable the callback function(DoSomething(), DoSomethingElse()..) to be called..

For checking this scenarios I've one scope variable say $scope.IsClickEnable = true/false;

So how can I accomplish this kind of behaviour?

I can use ng-disable to disable elements but it will only work on button kind of elements, so that will not work in my scenario

And I can also check conditions in every function call but I want more specific way to accomplish this by overriding ng-click behavior or something..

like image 713
Rushi Soni Avatar asked Apr 21 '14 09:04

Rushi Soni


People also ask

How do I give conditions in NG disabled?

The ng-disabled directive sets the disabled attribute of a form field (input, select, or textarea). The form field will be disabled if the expression inside the ng-disabled attribute returns true. The ng-disabled directive is necessary to be able to shift the value between true and false .

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.


1 Answers

Do like this:

<button ng-click="!IsClickEnable||DoSomethingElse()">xyz</button> 

when the value of IsClickEnable is true then the function will be called on the button click then the function 'DoSomethingElse()' will be called else it will not get called

like image 186
Aniket Avatar answered Sep 20 '22 12:09

Aniket