Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从指令AngularJs访问控制器功能?_Javascript_Jquery_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 如何从指令AngularJs访问控制器功能?

Javascript 如何从指令AngularJs访问控制器功能?,javascript,jquery,angularjs,angularjs-directive,Javascript,Jquery,Angularjs,Angularjs Directive,在主范围内,我有一个ng重复运行 <table class="datatable" prevent-right-click visible="isVisible"> <tr ng-repeat="t in templateData"> <td confusedFunction="clickedonname(t)">{{t.templateName}}</td>

在主范围内,我有一个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> 

下面是第二种方法的简单示例:

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> 

您希望接收单击将信息发送到preventRightClick指令,对吗?是的,但它应该传递(t)作为参数。我需要指令中的重复对象@Julientassin如果是这样,我想第一种方法是将控制器函数中的事件$broadcast到$rootScope,指令中对应的$on。我不能使用$rootScope,因为这将是一个非常大的企业应用程序的一部分。我刚刚编写了简单的代码,以理解执行此操作的逻辑。第二种方法是使用一个属性,您将在指令中attrs.$observe并在控制器函数中修改它的值嘿,谢谢。但是如何在函数中传递t(来自ng repeat)值呢?我需要那个。您好,t是在current.selected中传递的,从ng repeat到指令。您可以在watch函数中影响它,并在指令中使用它。
<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> 
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
          })

    }
  };
});