我刚碰到这个问题,写了一个解决方案。你可以编写一个自定义指令,它由一个容器,一个按钮和一个带有文件类型的输入元素组成。使用CSS,然后将输入放置在自定义按钮上,但不透明度为0.您将容器的高度和宽度设置为按钮的偏移宽度和高度,并将输入的高度和宽度设置为容器的100%。
指令
angular.module('myCoolApp')
.directive('fileButton', function() {
return {
templateUrl: 'components/directives/fileButton/fileButton.html',
restrict: 'E',
link: function (scope, element, attributes) {
var container = angular.element('.file-upload-container');
var button = angular.element('.file-upload-button');
container.css({
position: 'relative',
overflow: 'hidden',
width: button.offsetWidth,
height: button.offsetHeight
})
}
};
});
玉模板,如果你使用的是如果你使用的HTML
<div class="file-upload-container">
<button class="file-upload-button"></button>
<input class="file-upload-input" id="file-upload" type="file" onchange="doSomethingWhenFileIsSelected()" />
</div>
的CSS
玉
div(class="file-upload-container")
button(class="file-upload-button") +
input#file-upload(class="file-upload-input", type='file', onchange="doSomethingWhenFileIsSelected()")
在HTML同一模板
.file-upload-button {
margin-top: 40px;
padding: 30px;
border: 1px solid black;
height: 100px;
width: 100px;
background: transparent;
font-size: 66px;
padding-top: 0px;
border-radius: 5px;
border: 2px solid rgb(255, 228, 0);
color: rgb(255, 228, 0);
}
.file-upload-input {
position: absolute;
top: 0;
left: 0;
z-index: 2;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
您可能希望在这里包含实际的javascript和来自jade的输出,而不是预处理的代码,因为更多的人可以提供帮助。一般来说,这意味着你在$ scope中触发的函数中调用了$ scope($ apply())。apply() – shaunhusain
此外,这似乎通常是一个糟糕的主意,尽管即使假定某个元素是操作DOM有没有使用指令的名称作为封装逻辑的手段并不好。 – shaunhusain
通过隐藏实际的并触发其他方法的点击,这是一种非常标准的文件输入方式。 问题是角度抛出一个错误,因为它适用于一个应用 – mcfedr