简体   繁体   中英

In Angular how do I $eval() a function and pass the element to parent function?

Question: How can I get var trapfocus() inside ParentCtrl to console.log(el) from the approrate element properly ?

HTML

  <div ng-controller="ParentCtrl">
     <div ng-repeat="tab in tabs" modaltab="trapfocus($event)"></div>   
  </div>

JS:

  .directive('modaltab', function() {
        return function(scope, element, attrs) {
            element.bind("keydown keypress", function(event) {
                if(event.which === 9) {
                    scope.$apply(function(){
                        scope.$eval(attrs.modaltab, {'event': event});
                    });
            });
        };
    })

Parent Ctrl

.controller('ParentCtrl', function(){ 
   var trapfocus = function(evt, el){console.log(el)};
   $scope.tabs = [1,2,3,4];

 });

No need to $eval . Declare this attribute on the scope of the directive:

scope: {
   modaltab: "&"
}

Then, invoke it like so from the link function:

modaltab({'$event': event});

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