如何使用angularjs触发ng-click中另一个元素的点击事件? [英] How can I trigger the click event of another element in ng-click using angularjs?
本文介绍了如何使用angularjs触发ng-click中另一个元素的点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 button
触发 元素的点击事件.
I'm trying to trigger the click event of the <input type="file">
element from the button
.
<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>
通常的做法是隐藏被称为 的丑陋野兽,并通过其他方式触发它的点击事件.
It's common practice to hide the uglified beast known as <input type=file>
and trigger it's 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-click中另一个元素的点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文