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

时间:2014-12-09 01:21:55

标签: javascript angularjs angularjs-directive angularjs-scope eval

问题: 如何正确地从批准元素中var trapfocus() ParentCtrlconsole.log(el)

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});
                    });
            });
        };
    })

父Ctrl

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

 });

1 个答案:

答案 0 :(得分:1)

无需$eval。在指令的范围内声明此属性:

scope: {
   modaltab: "&"
}

然后,从链接函数中调用它:

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