2016-07-12 76 views
-5
<div> 
    <input id="txt" type="text" placeholder="Choose File" /> 
    <input type="file" id="selectedFile" class="hidden" onchange="ChangeText(this, 'txt');" /> 
    <span> 
     <button type="button" onclick="document.getElementById('selectedFile').click();">Browse</button> 
    </span> 
</div> 

我只想在角度js中的點擊事件。誰能告訴?GetElementById等效於角js

我只想在角度js中的點擊事件。 任何人都可以告訴? 代碼在評論

+0

+1

請更清楚地描述問題。如果你想在angular角度點擊事件,那麼需要使用ngclick事件 –

+0

我的代碼是在js中,在瀏覽按鈕中,我給了onclick事件,以便它將像這樣獲取selectedfile id元素, 所以我想要角度js中的那個函數 –

回答

0

好的,我可能沒有完全讀完你的問題。現在我有,我認爲這是你在找什麼:https://plnkr.co/edit/pfjU5B17qCzwHKoG4iTN?p=preview

.controller('Upload', ['$scope', function($scope) { 
    this.input; 

    this.browse = function() { 
     this.input.click(); 
    }; 
}]) 
.directive('fileUpload', [function() { 
    return { 
     'restrict':'E', 
     'controller':'Upload', 
     'controllerAs':'upload', 
     'templateUrl': './upload.html', 
     'link':function($scope, $element, $attribute, upload) { 
      upload.input = $element.find('input')[1]; 
     } 
    }; 
}]); 

在該指令的鏈接功能我注射輸入到控制器。控制器只需將一個點擊事件觸發到節點中,從而調用文件瀏覽器。

編輯:我個人不使用輸入來顯示文件,而是顯示所有選定的文件更寬泛的列表,並預覽如果可能的話。這樣,你只需要使用第一個輸入:)

對不起,沒有更好地閱讀你的問題,希望這有助於。

0

這不是角度的方式,DOM應該在您的代碼中調用函數,而不是您的代碼輪詢DOM中的事件。

<script> 
    $('#btn').click(function() { 
    doTheThing(); 
    }); 
</script> 
<button id="btn">Do the thing!</button> 

<my-button ng-click="{{ MyBtnCtrl.doTheThing() }}">Do the thing!</my-button> 

與控制器

function MyButtonController() { 
    this.doTheThing = function() { 
    alert('I\'m doing the thing!') 
    }; 
} 

編輯: 正如我現在只看到你的代碼,你表現的代碼是沒有棱角的代碼。如果你想使用的角度,請查看以下鏈接:

我喜歡codeschool讓你開始,因爲它是互動性更強,並提供了一步一步的指導。

+0

而不是getElementById,我可以在角度js的函數內寫什麼 –

+0

您不知道。您應該使用控制器編寫指令,並且只需在DOM內調用控制器的功能即可。當使用角度時,你應該「永遠」不要在你的代碼中使用DOMNode。可能有一些例外情況,但始終旨在不在控制器中引用您的DOM。 – Pjetr

0
<div> 
    <input id="txt" type="text" placeholder="Choose File" /> 
    <input type="file" id="selectedFile" class="hidden" onchange="ChangeText(this, 'txt');" /> 
    <span> 
     <button type="button" onclick="click();">Browse</button> 
    </span> 
</div> 

$scope.click = function() { 
     document.getElementById('selectedFile').click() 
}; 
+0

裏面的功能你使用的文件。的getElementById( 'selectedFile')。點擊()。我們可以在不使用getElementByID的情況下執行相同的功能 –