简体   繁体   English

Angular如何设置输入类型文件的样式

[英]Angular how to style input type file

I have already seen many questions about style an input type file but I can't find out to do It in angular. 我已经看到很多关于样式输入类型文件的问题,但我无法找到角度来做它。

So how can I style inside an angular app an input type file? 那么如何在角度应用程序内部输入输入类型文件?

This is my code inside and html page: 这是我的代码里面和html页面:

<input type="file" ngf-select ng-model="vm.attachment" name="file" accept="application/pdf" ngf-max-size="5MB" ngf-model-invalid="errorFile">

I have already tried with (after my input file hidde): 我已经尝试过(在输入文件hidde之后):

<md-button id="uploadButton" class="md-raised md-primary"> Choose Files </md-button>

link (scope, element, attrs) {
  const input   = element.find('#fileInput');
  const button = element.find('#uploadButton');

  if (input.length && button.length) {
    button.click((e) => input.click());
  }
}

But it doesn't work, my link function doesn't work because I'm using controller and inside the page retrieve error if i put this function 但它不起作用,我的链接功能不起作用,因为我使用控制器和页面内部检索错误,如果我把这个功能

You can use the ngf-select directive on a button - check the example from their github doc page: https://jsfiddle.net/danialfarid/xxo3sk41/590 您可以在按钮上使用ngf-select指令 - 从他们的github文档页面查看示例: https//jsfiddle.net/danialfarid/xxo3sk41/590

The ng-model file has a name attribute that you can use to display the filename: ng-model文件有一个name属性,可用于显示文件名:

<button ngf-select ng-model="picFile" accept="image/*"> Select Picture</button>
File name: {{picFile.name}}

It's not common to style a input file. 设置输入文件的样式并不常见。 I prefer to make it hidden, set a custom component and point its events to the input file. 我更喜欢隐藏它,设置自定义组件并将其事件指向输入文件。 So, I can style this component as much as we want. 所以,我可以根据需要设计这个组件的样式。

Input file hidden 隐藏输入文件

Make it hidden with display: none 使用display:none隐藏它

<input type="file" id="input-file" name="file" style="display: none">

My custom component 我的自定义组件

Add an onclick event, so a click here will point to our input file. 添加一个onclick事件,所以点击这里将指向我们的输入文件。

<md-button onclick="document.querySelector('#input-file').click()" class="md-raised md-primary"> Choose Files </md-button>

That's it, until here the job is done. 就是这样,直到这里工作完成。 But, if we also want to catch the document's name add onchange event to the input file. 但是,如果我们还想捕获文档的名称,请将onchange事件添加到输入文件中。

<input type="file" onchange="angular.element(this).scope().onchange(this)" id="input-file" name="file" style="display: none">
<span>{{theFile.name}}</span>
$scope.onchange = function (sender) {
  $scope.$apply(function($scope) {
    $scope.theFile = sender.files[0]; 
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM