2017-07-07 26 views
1

我有模板看起來像這樣(有許多情況下,模板是這樣):

<span> 
    <button type="button" ng-click="uploadPdf($event)"> 
    <i class="fa fa-file"></i> 
    <input type="file" class="pdfDoc" ng-show="false"> 
    </button> 
</span> 

的想法是,當按鈕用戶點擊後,input[type="file"]應觸發(點擊)。

我想首先使這樣的:

$scope.uploadPdf = function(e) { 
    e.currentTarget.lastElementChild.click(); 
}; 

和它的作品,但控制檯上我嚷嚷$申請仍在進行中,所以我找到了解決方案,其中提出的$timeout使用,所以我包裹上面的代碼,以$timeout

$scope.uploadPdf = function(e) { 
    $timeout(function() { 
    e.currentTarget.lastElementChild.click(); 
    }, 0, false); 
}; 

這出於某種原因運行無限。

有沒有人爲什麼會發生這種情況以及其他可能的解決方案?

謝謝。

回答

1

你可以只檢查被點擊了什麼元素,如果是輸入 - 什麼都不做

$scope.uploadPdf = function(e) { 
    if(e.target.tagName.toLowerCase() == 'input'){ 
    return false; 
    } 
    e.currentTarget.lastElementChild.click(); 
}; 
1

1)您點擊上用onclick UploadPdf

2)你觸發按鈕內點擊(其子元素)

3)點擊事件冒泡回升到按鈕按鈕

4)轉到點1 :)

並將該溶液可以是

ng-click="$event.stopPropagation();" 

應用於輸入元素

相關問題