简体   繁体   English

角。 关闭图像裁剪器后,手风琴不会从另一个面板打开相同的图像

[英]Angular. After closing image cropper does not open the same image from another panel in accordion

In accordion content there is a functionality to upload cropped image.在手风琴内容中有上传裁剪图像的功能。 When I clicked the icon to upload it opens modal for image cropping, but when I close the modal instead of "Done" in second time for the same image it does not open the modal.当我单击图标上传时,它会打开用于图像裁剪的模态,但是当我第二次关闭模态而不是“完成”时,它不会打开模态。

Html Input HTML 输入

<label [for]="'image-input-' + i">
  <span class="icon">
    <i class="fa-solid fa-circle-plus" title="Upload new image"></i>
  </span>
</label>

<input hidden #imageInput [id]="'image-input-' + i" type="file" accept="image/*"                               
      (change)="onFileChange($event, promotion.promotion,'image')" />

Cropper农作物

<ng-template #ImageCropper let-modal>
    <div class="p-2 cropper">
        <h5 class="modal-title" id="exampleModalLabel">
            Crop Selected
            Image</h5>
        <image-cropper [maintainAspectRatio]="true"
            [aspectRatio]="6 / 3" format="png"
            [imageChangedEvent]="imageCropperEventAttached"
            (imageCropped)="ImageCropped($event)">
        </image-cropper>
    </div>
    <div class="d-flex">
        <button type="button"
            class="btn btn-danger text-white px-3 py-2 m-2 ms-auto"
            (click)="onThumbnailCropperCloseClick()">
            Close
        </button>
        <button type="button"
            class="btn btn-primary px-3 py-2 m-2"
            (click)="processFile(imageInput, selectedData, 'image')">
            Done
        </button>
    </div>
</ng-template>

typescript打字稿

onFileChange(event: any, pr: any, imgType: any): void {
    this.imageCropperEventAttached = event;
    this.popup = this.modalService.open(this.imageCropModel);
    this.selectedData = pr;
  }

I tried to add @ViewChild('imageInput') imageInput: ElementRef;我试图添加@ViewChild('imageInput') imageInput: ElementRef;

and set value null on closing but it does not work properly when I try to upload the same image from another panel .并在关闭时设置值 null 但是当我尝试从另一个面板上传相同的图像时它无法正常工作。

onThumbnailCropperCloseClick() {
    this.popup.close();
    this.imageInput.nativeElement.value = '';
  }

Stackblitz 堆栈闪电战

You have two options to address this issue.您有两种选择来解决此问题。

  1. Force the user to take action by disabling backdrop dismiss.通过禁用背景关闭来强制用户采取行动。

  2. Listen to result event triggered, when user dismisses the modal without pressing a button.当用户在不按下按钮的情况下关闭模式时,侦听触发的result 事件

onFileChange(event: any, data): void {
    console.log(event);

    this.imageCropperEventAttached = event;
    this.popup = this.modalService.open(this.imageCropModel);
    
    // option 1: force user to take action, by disabling backdrop dismiss
    // this.popup = this.modalService.open(this.imageCropModel, {backdrop: 'static', keyboard: false});
    this.selectedData = data;
    console.log(data);

    // option 2: listen to result event emitted from modal dismiss
    this.popup.result.then(() => {}, () => this.onThumbnailCropperCloseClick());
  }

Stackblitz demo Stackblitz 演示

Every time ViewChild returns the first panel because Angular will find the first instance of the panel.每次 ViewChild 返回第一个面板,因为 Angular 会找到该面板的第一个实例。 Change ViewChild to ViewChildren for getting all of the panels.将 ViewChild 更改为 ViewChildren 以获取所有面板。

@ViewChildren('imageInput') imageInput: any;

And also set empty value to all panels in onThumbnailCropperCloseClick()并且还在 onThumbnailCropperCloseClick() 中为所有面板设置空值

onThumbnailCropperCloseClick() {
    this.popup.close();
    this.imageInput.forEach((panel: ElementRef) => panel.nativeElement.value = '');
}

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

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