【发布时间】:2018-02-22 14:37:19
【问题描述】:
我有这个指令绑定和事件到输入文件元素,并在控制器上调用一个函数
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
};
我需要将指令所在对象的参数传递给指令在更改事件上执行的函数。
我知道我可以在元素上使用类似 campo="{{campo}}" 的东西,并在指令上像这样 attrs.campo 那样捕获它,但我不知道如何进行此绑定
element.on('change', onChangeHandler);
传递参数 - 我总是得到这个错误
jqLite#on() 不支持
selector或eventData。
这是Plunkr
【问题讨论】:
标签: javascript angularjs angularjs-directive