Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ng-disabled not working with list items

I am facing a problem with disabling a item in the list.

<div id="searchdropdown">
    <ul>
        <li>list1</li>
        <li ng-disabled="someCondition" ng-click="changeStatus()">list2</li>
        <li>list3</li>
    </ul>
</div>

It does not work with ng-disabled.

I also tried with:

<li ng-class="disabled:someCondition" click="changeStatus()"> list2
</ li> 

It also does not work. Can anyone suggest something?

like image 307
Hmahwish Avatar asked Jan 22 '15 07:01

Hmahwish


People also ask

How to use ng-disabled in AngularJS?

Definition and UsageThe 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 .

How to disable element in AngularJS?

The ng-disabled Directive in AngularJS is used to enable or disable the HTML elements. If the expression inside the ng-disabled attribute returns true then the form field will be disabled or vice versa. It is usually applied on the form field (i.e, input, select, button, etc).

How to disable a div in AngularJS?

-- enclosing a div within the fieldset and set ng-disabled to fieldset should work.

How to make button enable and disable in AngularJS?

Sometimes, we need to disable the button, and link on the click event. This task can be accomplished by implementing the ng-disabled directive that is used to enable or disable HTML elements. Parameter value: expression: It represents an expression that returns true if it sets the disabled attribute for the element.


Video Answer


1 Answers

I'm assuming it is a search box of some kind.

ngDisabled is really used for interactive elements and not list items.

You can use ng-if or ng-hide to remove those items completely from the list:

<li ng-if="!condition" ng-click="changeStatus()">item</li>
<li ng-hide="condition" ng-click="changeStatus()">item</li>

You can use ngClass to apply a specific class when disabled to make it appear disabled:

<li ng-class="{'disabled':condition}" ng-click="changeStatus()">item</li>

If you want an item to be visible but not click-able, you may have to do a hack like re-opening the search box if the item is disabled or sinking the event.

like image 192
JGefroh Avatar answered Sep 23 '22 07:09

JGefroh