加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 09:01:04 所属栏目:安全 来源:网络整理
导读:我有一些麻烦绑定控制器中定义的函数与指令中的回调函数。我的代码看起来像下面: 在我的控制器: $scope.handleDrop = function ( elementId,file ) { console.log( 'handleDrop called' );} 然后我的指令: .directive( 'myDirective',function () { retur
我有一些麻烦绑定控制器中定义的函数与指令中的回调函数。我的代码看起来像下面:

在我的控制器:

$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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读