我的AngularJS
项目存在一个奇怪的问题。我有2 buttons
触发inputs
以下。
<button type="button" class="btn btn-primary" data-ng-click="triggerInput('B1')">B1</button>
<input id="b1Input" type="file" accept="image/*" class="hide" onchange="angular.element(this).scope().changeImage(this)" />
<button type="button" class="btn btn-primary" data-ng-click="triggerInput('B2')">B2</button>
<input id="b2Input" type="file" accept="image/*" class="hide" onchange="angular.element(this).scope().changeImage(this)" />
我尝试调试JS
代码,但似乎没问题。不幸的是,有时我必须单击所选按钮几次(2-4)才能打开输入。也许jQuery
选择器不好?
以下是代码:
$scope.triggerInput = function (type) {
$timeout(function () {
var selector = "";
switch (type) {
case "B1":
selector = "b1Input";
break;
case "B2":
selector = "b2Input";
break;
}
$("input[id='" + selector + "']").trigger("click");
});
};
答案 0 :(得分:1)
您不应将此行(位于下方)用于控制器。我想你可以写一个directive来填充这个。
$("input[id='" + selector + "']").trigger("click");
更正您的代码
<button type="button" class="btn btn-primary" data-ng-click="triggerInput('B2',$event)">B2</button>
$scope.triggerInput = function (type,e) {
$timeout(function () {
var selector = "";
switch (type) {
case "B1":
selector = "b1Input";
break;
case "B2":
selector = "b2Input";
break;
}
angular.element(e.target).siblings('#'+selector).trigger('click');
//or simply write this
//$("#"+selector).trigger("click");
});
};