AngularJS directive binding a function with multiple arguments

ぐ巨炮叔叔 提交于 2019-12-28 03:29:05

问题


I'm having some trouble binding a function defined in a controller with a callback function in a directive. My code looks like the following:

In my controller:

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

Then my directive:

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

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

And in my html page:

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

Having no luck with the code above. From what I've read in various tutorials I understand I'm supposed to specify the arguments in the HTML page?


回答1:


There is one small mistake in your code, please try the code below and it should work for you

<!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, elem, 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>



回答2:


Alternative method that will survive minification

leave your html as it was:

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

change the call to:

scope.onDrop()('123','125')

notice the extra opening and closing parenthesis given to onDrop. This will instantiate the function instead of injecting the function's code.

Why is it better

  1. changing the parameters' names in the handleDrop() definition (or even adding some more, if you handle it correctly) will not make you change each of the directives injections in the html. much DRYer.

  2. as @TrueWill suggested, i'm almost sure the other solutions will not survive minification, while this way code stays with maximum flexibility and is name agnostic.

another personal reason is the object syntax, which makes me write much more code:

functionName({xName: x, yName: y})

(and adding the function signature in every directive call)

as oppose to

functionName()(x,y)

(zero maintenance to your html)

i found this great solution here.

hope that helps!



来源:https://stackoverflow.com/questions/18973507/angularjs-directive-binding-a-function-with-multiple-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!