2016-07-12 40 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的情况下执行相同的功能 –