我如何从指令angularJS访问控制器函数



在主范围上,我有一个ng重复运行。

<table class="datatable" prevent-right-click visible="isVisible">
                <tr ng-repeat="t in templateData">
                    <td confusedFunction="clickedonname(t)">{{t.templateName}}</td>
                    <td>{{t.templatePath}}</td>
                </tr>
            </table> 

预防右键是一个自定义上下文对其中的评论框,它在第一个TD元素上对相应的右键单击元素进行注释。无论如何,我是否可以编写一个函数,该函数获取重复的元素并通过指令传递,以便可以将评论记录在相应的元素上?另外,预防右键具有孤立的范围。

这就是我的指示代码。

app.directive('preventRightClick', function() {
    return {
        restrict: 'A',
        scope: {
            visible: '='
        },
        link: function($scope, $ele) {
            $ele.on('contextmenu', '*', function(e) {
                e.preventDefault();
                $scope.$apply(function () {
                    $scope.visible = true;
                    $('.parented').css({right:50, top:50}).show();
                })
                e.stopPropagation();
            });
            $(document).on('click', '*', function (e) {
                if ($(e.target).parents('.parented').length  > 0) {
                }
                else{
                    $('.parented').hide()
                    $ele.off('contextmenu', '*', function(){
                        console.log('Context menu off')
                    })
                }
            })
            $scope.confusedFunction = function(t){
                console.log(t.templateName)
                console.log('coming from clickedonname')
            }
        }
    };
})

这是第二种方式的简单示例:

html:

<table class="datatable" prevent-right-click visible="isVisible" foo=current.selected ng-init="current = {}">
  <tr ng-repeat="t in templateData">
    <td ng-click="current.selected = t">{{t.templateName}}</td>
    <td>{{t.templatePath}}</td>
  </tr>
</table> 

指示JS:

app.directive('preventRightClick', function() {
    return {
        restrict : "A",
        scope: {
          foo: '=foo',
        },
        link : function (scope, element, attrs) {
          scope.$watch('foo', function(n) {
            // This code will be triggered with the new t in new value on each td click
          })
    }
  };
});

最新更新