我怎么可以触发使用angularjs在NG-点击另一个元素的单击事件? [英] how can i trigger the click event of another element in ng-click using angularjs?

查看:767
本文介绍了我怎么可以触发使用angularjs在NG-点击另一个元素的单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图触发&LT的Click事件;输入类型=文件> 按钮元素

<input id="upload"
    type="file"
    ng-file-select="onFileSelect($files)"
    style="display: none;">

<button type="button"
    ng-click="angular.element('#upload').trigger('click');">Upload</button>

其通常的做法是隐藏已知的变丑兽&LT;输入类型=文件方式&gt; ,并通过其他方法触发其click事件

Its common practice to hide the uglified beast known as <input type=file> and trigger its click event by some other means.

推荐答案

如果你的输入和按键是兄弟(他们都是你的情况OP):

If your input and button are siblings (and they are in your case OP):

<input id="upload"
    type="file"
    ng-file-select="onFileSelect($files)"
    style="display: none;">

<button type="button" uploadfile>Upload</button>

使用指令,你点击按钮绑定到像这样的文件输入:

Use a directive to bind the click of your button to the file input like so:

app.directive('uploadfile', function () {
    return {
      restrict: 'A',
      link: function(scope, element) {

        element.bind('click', function(e) {
            angular.element(e.target).siblings('#upload').trigger('click');
        });
      }
    };
});

这篇关于我怎么可以触发使用angularjs在NG-点击另一个元素的单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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