简体   繁体   English

Angular 指令绑定事件并将参数传递给控制器​​函数

[英]Angular directive bind event and pass parameter to controller function

I have this directive that binds and event to input file elements, and calls a function on the controller我有这个指令绑定和事件到输入文件元素,并在控制器上调用一个函数

appFuncionario.directive('onFileChange', function () {
   return {
      restrict: 'A',
      link: function (scope, element, attrs) {

         var onChangeHandler = scope.$eval(attrs.onFileChange);
         //this is where I want to send the parameter
         element.on('change', onChangeHandler);

         element.on('$destroy', function () {
            element.off();
         });

      }
   };
});


<input ng-show="campo.codTipoCampo === 'InputFile'" id="{{campo.id}}" type="file" campo="{{campo}}" on-file-change="uploadFile" />

 //Function in the controller - this is where I want to get a parameter from the directive
 $scope.uploadFile = function (parameter) {

        //do something here
};

I need to pass a parameter from the object where the directive is in, to the function executed by the directive on the change event.我需要将参数从指令所在的对象传递给指令在更改事件上执行的函数。

I know I can use something like this campo="{{campo}}" on the element, and catch it like this attrs.campo on the directive, but I can't figure it out how to do this binding我知道我可以在元素上使用类似campo="{{campo}}"的东西,并在指令上像attrs.campo一样捕获它,但我不知道如何进行这种绑定

element.on('change', onChangeHandler);

passing a parameter - I always get this error传递参数 - 我总是收到这个错误

jqLite#on() does not support the selector or eventData . jqLit​​e#on() 不支持selectoreventData

Here's a Plunkr这是一个Plunkr

Is this what you're looking for?这是你要找的吗? I'm trying to figure out the bigger picture here as to why you're trying to stuff in variables in the directive level and not just using the controller for what it is intended for but take a look at the Fiddle below and lmk if that's what you're looking for.我试图在这里找出更大的图景,以了解为什么您要尝试在指令级别中填充变量,而不仅仅是将控制器用于它的用途,而是查看下面的Fiddle和 lmk 如果是这样你在找什么。

 // Code goes here var app = angular.module('fiddle', []); app.controller('MainCtrl', function($scope) { $scope.teste = function(campo) { alert("Campo: " + campo); }; }); app.directive('onFileChange', function() { return { restrict: 'A', link: function(scope, element, attrs) { var onChangeHandler = scope.$eval(attrs.onFileChange); element.on('change', function() { onChangeHandler(attrs.campo); }); element.on('$destroy', function() { element.off(); }); } }; });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="fiddle"> <div ng-controller="MainCtrl"> <!-- Attribute campo is the variable stuffed into the controller function via the directive on change event --> <input type="file" campo="{{'I did it!'}}" on-file-change="teste" /> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM