'AngularJS - ng-disabled is not disabling my element
I´m trying to hide an option if the condition exists, but the option is still there.
Here is my code:
<li role="presentation"><a role="menuitem" tabindex="-1" ng-disabled="disAplicar" ng-if="!hideBtnAplicar" ng-click="existenciaCero()">Aplicar Existencia Cero</a></li>
And my JS is:
$scope.disAplicar = $scope.datos.tipo == 'informativo' ? true : false;
Could you help me to find which the error is?
Thanks in advance.
Solution 1:[1]
If you want to simulate disabling an a element, you'll need to do it in two ways: styling and behavior. For the styling, have something like a disabled CSS class that applies programmatically using ng-class="{ 'disabled': disAplicar }". That will make it look disabled, but to make it act disabled as well, make sure your ng-click function considers the same condition.
$scope.existenciaCero = function() {
if ($scope.disAplicar) {
return;
}
// the rest of your code
}
And in the event that you're using href on the a instead of a click event, you'd want something like this, in addition to the disabled styling I mentioned:
<a ng-href="{{ disAplicar ? '/someUrl' : '#' }}">My link</a>
Solution 2:[2]
In this case you should really use a button rather than an a tag, you could style the button like an a tag here with CSS.
<li role="presentation"><button role="menuitem" tabindex="-1" ng-disabled="disAplicar" ng-if="!hideBtnAplicar" ng-click="existenciaCero()">Aplicar Existencia Cero</button></li>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jacob Stamm |
| Solution 2 | GQ. |
