简体   繁体   English

在Angular中,我如何$ eval()一个函数并将元素传递给父函数?

[英]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 ? 问:我如何获得var trapfocus()ParentCtrlconsole.log(el)从正常的approrate元素?

HTML HTML

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

JS: 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 上层Ctrl

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

 });

No need to $eval . 无需$eval Declare this attribute on the scope of the directive: 在指令的范围内声明此属性:

scope: {
   modaltab: "&"
}

Then, invoke it like so from the link function: 然后,从link函数像这样调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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