简体   繁体   English

如何更改角度的个人资料图片?

[英]How to change profile picture in angular?

I am making angular 6 application where i am using image upload option which has, 我正在使用我的图像上传选项制作Angular 6应用程序,

Html: HTML:

<img [src]="url ? url : 'https://www.w3schools.com/howto/img_avatar.png'"> <br/>
<input type='file' (change)="onSelectFile($event)">

Ts: Ts:

  onSelectFile(event) {
    if (event.target.files && event.target.files[0]) {
      var reader = new FileReader();

      reader.readAsDataURL(event.target.files[0]); // read file as data url

      reader.onload = (event) => { // called once readAsDataURL is completed
        this.url = event.target.result;
      }
    }
  }

Working stackblitz: https://stackblitz.com/edit/angular-file-upload-preview-ekyhgh 可工作的stackblitz: https ://stackblitz.com/edit/angular-file-upload-preview-ekyhgh

Here the thing i have made is given html input file which displays choosen file as text but i need to have it on click over the profile image, the folder needs to be displayed from local (which will appear on click choosen file button in normal).. 在这里,我给的东西是html input file ,该html input file将所选文件显示为文本,但是我需要在个人资料图片上单击时显示它,该文件夹需要从本地显示(通常会在单击所选文件按钮上显示) ..

In order to confuse much i am having a link which i am in the need was https://codepen.io/anon/pen/PXjaJv 为了使我感到困惑,我有一个链接,我需要的是https://codepen.io/anon/pen/PXjaJv

Here you can see on hover over the image the text appears as Drag your photo here or browse your computer .. (The same text appears in the given link default because there is no picture there but i am in the need of hover only because i am having avatar image already so on hover over any image i need to display this text of changing profile picture).. 在这里,您可以将鼠标悬停在图像上,文字显示为Drag your photo here or browse your computer ..(相同的文字出现在给定的链接默认值中,因为那里没有图片,但是我只需要悬停是因为我我已经有头像图像,因此将鼠标悬停在任何图像上,我需要显示此更改个人资料图片的文本。)

Ignore cropping and dropping of the image in this link https://codepen.io/anon/pen/PXjaJv but only thing i am need is UI changes like on hover make an overlay text and on click the text make browse of pictures from computer to change and then change the profile picture with delete option which will return back to avatar image itself (if deleted).. 忽略此链接中图像的裁剪和拖放https://codepen.io/anon/pen/PXjaJv,但是我唯一需要做的就是UI更改,例如悬停时会生成覆盖文本,然后单击该文本可从计算机浏览图片更改,然后使用删除选项更改个人资料图片,这将返回到头像图像本身(如果已删除)。

Kindly help me to achive the result using pure angular/typescript way without jquery. 请帮助我使用没有jquery的纯角度/打字稿方式来达到结果。

You should use label tag with proper for attribute. 您应该使用带有适当的for属性的label标签。 for attribute must contain id of input tag. for属性必须包含input标签的id

Look at example. 看例子。

<label class="hoverable" for="fileInput">
  <img [src]="url ? url : 'https://www.w3schools.com/howto/img_avatar.png'"> 
  <div class="hover-text">Choose file</div>
  <div class="background"></div>
</label>
<br/>
<input id="fileInput" type='file' (change)="onSelectFile($event)">
<button *ngIf="url" (click)="delete()" >delete</button>

Example on stackblitz . 关于stackblitz的示例。

Although Above answers are correct I would like to add, 尽管以上答案是正确的,但我想补充一下,

.css .css

p {
  font-family: Lato;
}

.image-container {
  position: relative;
  display: inline-block;
  text-align: center;
}

img {
  height: 200px;
  width: 200px;
  border: 5px solid #000;
  border-radius: 50%;
}

.select-profile-picture {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999;
  opacity: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

.message {
  position: absolute;
  width: 90%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, 100%);
  transition: all 1s;
  opacity: 0;
}

.image-container:hover .message {
  transform: translate(-50%, -50%);
  opacity: 1;
}

.html .html

<div class="image-container">
 <img [src]="url ? url : 'https://www.w3schools.com/howto/img_avatar.png'" class=""> <br/>
 <input type='file' (change)="onSelectFile($event)" class="select-profile-picture">
 <span class="message">Tap here to select your picture</span>
</div>

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

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