angular - access the element from ng-disabled

那年仲夏 提交于 2019-12-11 04:28:29

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/37429563/angular-access-the-element-from-ng-disabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!