简体   繁体   中英

Pass element using ng-show AngularJS

HTML

<li ng-show="sample($event)" TestLi</li>

Javascript

$scope.sample = function($event){ //$event is undefined
//do something 
}

I've only tried passing the html element using ng-click but is there a way to pass it using ng-show?.

The attribute ng-show, ng-hide and ng-if usually evaluate expression and not function. You can read about it here:

https://docs.angularjs.org/api/ng/directive/ngShow

However, if you really wish to get the target element, you may try writing a very simple directive.

<li ng-show="sample($event)" getTarget> TestLi</li>

app.directive("getTarget", function() {
    return {
        link: function(scope, element, attrs) {
            console.log(element);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM