Search code examples
htmlangularjshtmlelements

angular - access the element from ng-disabled


I want to pass this (the element) - a button to my controller by ng-disable. here is my HTML:

<button type ="button" class = "btn btn-default" ng-click= "open()" ng-disabled = "checkRowId(this)">
</button>

And my controller :

$scope.checkRowId = function(btn){
   console.log(btn); //undefined
}

The log shoes undefined, is there a way to pass a element like a button via ng-disabled?


Solution

  • There is no direct way to pass element via ng-disabled. You can create one directive say "disabled-ele" and put your element disabling logic there.

    Example Code:

    .directive('disabledEle', function() {
                        return {
                            restrict: 'EA',
                            link: function(scope, element, attrs) {
                                if(attrs.disabledEle.toLowerCase() === 'true') {
                                    element.attr('disabled', 'disabled');
                                } else {
                                    element.removeAttr('disabled');
                                }
                            }
                        }
                    });
    
    <button type ="button" value="Click" class = "btn btn-default" disabled-ele="false">Click
                        </button>