从输入的onchange调用函数 [英] Call a function from input's onchange

查看:95
本文介绍了从输入的onchange调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用控制器作为语法.

I'm using controller as syntax.

当我的文件输入更改时,我想触发一个功能.

And when my file input changed, i want to trigger a function.

该怎么做?

下面是我的带有$ scope语法的代码

below is my code with $scope syntax

<input class="upload" type="file" accept="image/*" ng-model="image"
                onchange="angular.element(this).scope().uploadImage(this)">

推荐答案

As found here, you could use a custom directive to listen for changes in your input file.

view.html:

view.html:

<input type="file" custom-on-change="uploadFile">

controller.js:

controller.js:

app.controller('myCtrl', function($scope){
    $scope.uploadFile = function(event){
        var files = event.target.files;
    };
});  

directive.js:

directive.js:

app.directive('customOnChange', function() {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      var onChangeHandler = scope.$eval(attrs.customOnChange);
      element.bind('change', onChangeHandler);
    }
  };
});

查看JSFIDDLE

这篇关于从输入的onchange调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆