微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AngularJS指令绑定具有多个参数的函数

我有一些麻烦绑定控制器中定义的函数与指令中的回调函数。我的代码看起来像下面:

在我的控制器:

$scope.handleDrop = function ( elementId,file ) {
    console.log( 'handleDrop called' );
}

然后我的指令:

.directive( 'myDirective',function () {
    return {
      scope: {
        onDrop: '&'
      },link: function(scope,elem,attrs) {
        var myFile,elemId = [...]

        scope.onDrop(elemId,myFile);
      }
    } );

在我的html页面

<my-directive on-drop="handleDrop"></my-directive>

没有运气上面的代码。从我在各种教程中读到的,我明白我应该在html页面中指定参数?

非常感谢任何帮助!

约瑟夫。

您的代码中有一个错误,请尝试下面的代码,它应该为您工作
<!doctype html>
<html ng-app="test">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script>

  </head>
 <body ng-controller="test" >    


<!-- tabs -->
<div my-directive on-drop="handleDrop(elementId,file)"></div>

 <script>
     var app = angular.module('test',[]);

     app.directive('myDirective',function () {
         return {
             scope: {
                 onDrop: '&'
             },link: function (scope,attrs) {
                 var elementId = 123;
                 var file = 124;
                 scope.onDrop({elementId:'123',file:'125'});

             }
         }
     });

     app.controller('test',function ($scope) {
         alert("inside test");
         $scope.handleDrop = function (elementId,file) {
             alert(file);
         }
     });

   </script>
</body>


</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐