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

自定义选择图片的方法

  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
  14. [files]="files"
  15. [multiple]="multiple"
  16. [selectable]="files.length < 5"
  17. (onChange)="fileChange($event)"
  18. (onImageClick)="imageClick($event)"
  19. (onAddImageClick)="addImageClick($event)"
  20. ></ImagePicker>
  21. `
  22. })
  23. export class DemoImagePickerCustomComponent {
  24. files = data.slice(0);
  25. multiple = false;
  26. fileChange(params) {
  27. const { files, type, index } = params;
  28. this.files = files;
  29. }
  30. addImageClick(e) {
  31. e.preventDefault();
  32. this.files = this.files.concat({
  33. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
  34. });
  35. }
  36. imageClick(params) {
  37. console.log(params);
  38. }
  39. }

自定义文件类型

  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
  14. [files]="files"
  15. [multiple]="multiple"
  16. [selectable]="files.length < 5"
  17. [accept]="'image/gif,image/jpeg,image/jpg,image/png'"
  18. (onChange)="fileChange($event)"
  19. (onImageClick)="imageClick($event)"
  20. (onImageChange)="ImageChange($event)"
  21. ></ImagePicker>
  22. `
  23. })
  24. export class DemoImagePickerAcceptComponent {
  25. files = data.slice(0);
  26. multiple = false;
  27. multipleTab = 0;
  28. ImageChange(params) {
  29. const { files, type, index } = params;
  30. this.files = files;
  31. }
  32. fileChange(event) {
  33. console.log(event);
  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-length',
  12. template: `
  13. <ImagePicker
  14. [files]="files"
  15. [length]="6"
  16. [selectable]="files.length < 7"
  17. (onImageClick)="imageClick($event)"
  18. (onImageChange)="imageChange($event)"
  19. (onAddImageClick)="addImageClick($event)"
  20. ></ImagePicker>
  21. `
  22. })
  23. export class DemoImagePickerLengthComponent {
  24. files = data.slice(0);
  25. multiple = false;
  26. imageChange(params) {
  27. const { files, type, index } = params;
  28. this.files = files;
  29. }
  30. addImageClick(e) {
  31. e.preventDefault();
  32. this.files = this.files.concat({
  33. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg'
  34. });
  35. }
  36. imageClick(params) {
  37. console.log(params);
  38. }
  39. }

ImagePicker图片选择器 - 图1

API

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