ImagePicker图片选择器

注意:只是图片选择器,一般用于上传图片前的文件选择操作,但不包括图片上传的功能

代码演示

简单的图片选择组件

  1. import { Component } from '@angular/core';
  2. const data = [
  3. {
  4. url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg'
  5. },
  6. {
  7. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
  8. }
  9. ];
  10. @Component({
  11. selector: 'demo-image-picker-basic',
  12. template: `
  13. <div class="ip-segment-wrapper">
  14. <div class="ip-segment-btn"
  15. [ngClass]="{selected: multipleTab === 0}"
  16. (click)="changeMultiple(0)"
  17. >
  18. 切换到单选
  19. </div>
  20. <div class="ip-segment-btn"
  21. [ngClass]="{selected: multipleTab === 1}"
  22. (click)="changeMultiple(1)"
  23. >
  24. 切换到多选
  25. </div>
  26. </div>
  27. <ImagePicker [files]="files"
  28. [selectable]="files.length < 5"
  29. [multiple]="multipleTab === 1"
  30. (onChange)="fileChange($event)"
  31. (onImageClick)="imageClick($event)"
  32. ></ImagePicker>
  33. `,
  34. styles: [
  35. `
  36. .ip-segment-wrapper {
  37. display: flex;
  38. border-radius: 5px;
  39. overflow: hidden;
  40. min-height: 27px;
  41. opacity: 1;
  42. }
  43. .ip-segment-btn {
  44. display: flex;
  45. flex: 1;
  46. justify-content: center;
  47. align-items: center;
  48. color: #108ee9;
  49. font-size: 14px;
  50. line-height: 1;
  51. -webkit-transition: background 0.2s;
  52. transition: background 0.2s;
  53. position: relative;
  54. border: 1px solid #108ee9;
  55. width: 100%;
  56. box-sizing: border-box;
  57. border-left-width: 0;
  58. }
  59. .ip-segment-btn.selected {
  60. background: #108ee9;
  61. color: #fff;
  62. }
  63. .ip-segment-btn:nth-child(1) {
  64. border-left-width: 1px;
  65. border-radius: 5px 0 0 5px;
  66. }
  67. .ip-segment-btn:nth-child(2) {
  68. border-left-width: 1px;
  69. border-radius: 0 5px 5px 0;
  70. }
  71. `
  72. ]
  73. })
  74. export class DemoImagePickerBasicComponent {
  75. files = data.slice(0);
  76. multiple = false;
  77. multipleTab = 0;
  78. changeMultiple(value: number) {
  79. this.multipleTab = value;
  80. }
  81. fileChange(params) {
  82. console.log(params);
  83. const { files, type, index } = params;
  84. this.files = files;
  85. }
  86. imageClick(params) {
  87. console.log(params);
  88. }
  89. }

自定义选择图片的方法

  1. import { Component } from '@angular/core';
  2. const data = [
  3. {
  4. url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg'
  5. },
  6. {
  7. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
  8. }
  9. ];
  10. @Component({
  11. selector: 'demo-image-picker-custom',
  12. template: `
  13. <ImagePicker [files]="files"
  14. [selectable]="files.length < 5"
  15. [multiple]="multiple"
  16. (onChange)="fileChange($event)"
  17. (onImageClick)="imageClick($event)"
  18. (onAddImageClick)="addImageClick($event)"
  19. ></ImagePicker>
  20. `
  21. })
  22. export class DemoImagePickerCustomComponent {
  23. files = data.slice(0);
  24. multiple = false;
  25. fileChange(params) {
  26. const { files, type, index } = params;
  27. this.files = files;
  28. }
  29. addImageClick(e) {
  30. e.preventDefault();
  31. this.files = this.files.concat({
  32. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
  33. });
  34. }
  35. imageClick(params) {
  36. console.log(params);
  37. }
  38. }

自定义文件类型

  1. import { Component } from '@angular/core';
  2. const data = [
  3. {
  4. url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg'
  5. },
  6. {
  7. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
  8. }
  9. ];
  10. @Component({
  11. selector: 'demo-image-picker-accept',
  12. template: `
  13. <ImagePicker [files]="files"
  14. [selectable]="files.length < 5"
  15. [multiple]="multiple"
  16. [accept]="'image/gif,image/jpeg,image/jpg,image/png'"
  17. (onImageClick)="imageClick($event)"
  18. (onChange)="fileChange($event)"
  19. (onImageChange)="ImageChange($event)"
  20. ></ImagePicker>
  21. `
  22. })
  23. export class DemoImagePickerAcceptComponent {
  24. files = data.slice(0);
  25. multiple = false;
  26. multipleTab = 0;
  27. ImageChange(params) {
  28. const { files, type, index } = params;
  29. this.files = files;
  30. }
  31. fileChange(event) {
  32. console.log(event);
  33. }
  34. imageClick(params) {
  35. console.log(params);
  36. }
  37. }

自定义数量

  1. import { Component } from '@angular/core';
  2. const data = [
  3. {
  4. url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg'
  5. },
  6. {
  7. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
  8. }
  9. ];
  10. @Component({
  11. selector: 'demo-image-picker-length',
  12. template: `
  13. <ImagePicker [files]="files"
  14. [length]="6"
  15. [selectable]="files.length < 7"
  16. (onImageChange)="imageChange($event)"
  17. (onImageClick)="imageClick($event)"
  18. (onAddImageClick)="addImageClick($event)"
  19. ></ImagePicker>
  20. `
  21. })
  22. export class DemoImagePickerLengthComponent {
  23. files = data.slice(0);
  24. multiple = false;
  25. imageChange(params) {
  26. const { files, type, index } = params;
  27. this.files = files;
  28. }
  29. addImageClick(e) {
  30. e.preventDefault();
  31. this.files = this.files.concat({
  32. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
  33. });
  34. }
  35. imageClick(params) {
  36. console.log(params);
  37. }
  38. }

ImagePicker 图片选择器 - 图1

API

属性说明类型默认值
files图片文件数组,元素为对象,包含属性 url(必选, 可能还有id, orientation, 以及业务需要的其它属性Array[]
onChangefiles 值发生变化触发的回调函数, operationType 操作类型有添加,移除,如果是移除操作,则第三个参数代表的是移除图片的索引(files: Object, operationType: string, index: number): void
onImageClick点击图片触发的回调(index: number, files: Object): void
onAddImageClick自定义选择图片的方法(): void
onFail选择失败(msg: string): void
selectable是否显示添加按钮booleantrue
multiple是否支持多选booleanfalse
accept图片类型stringimage/*
length单行的图片数量number4
capture图片捕获设置, 具体请参考MDN中关于capture的说明boolean | stringfalse
disableDelete是否隐藏删除按钮booleanfalse