1

I am using ng2-file-upload for 2 different components but essentially the same code. I copy and pasted the component code that is working to the component code that is not. The issue is after I upload a file, nothing happens. I have to click upload several times before the uploader actually recognizes a file was uploaded. In the other component, it works perfectly.

Here is my component.ts code for the working file

import {Component, DoCheck, OnInit} from '@angular/core';
import {UiService} from "../../ui.service";
import {Transaction} from "../transaction.model";
import {TransactionService} from "../transaction.service";
import {FileUploader} from "ng2-file-upload";
import {UploadService} from "../../settings/account-settings/contracts/upload-service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-owner-post-eval',
  template: `
    <div class="modal is-active" [ngStyle]="styleObject('ownerPost')">
      <div class="modal-background"></div>
      <div class="modal-card">
        <header class="modal-card-head">
          <p class="modal-card-title">Post Evaluation</p>
        </header>
        <section class="modal-card-body">
          <form *ngIf="!uploaderLoading">
            <label class="label">Post Evaluation</label>
            <textarea class="textarea"
                      [(ngModel)]="ownerPostEval"
                      placeholder="Please leave a review of your items current state."
                      name="ownerPostEval"></textarea>
          </form>
          <div class="field image-upload" *ngIf="uploader?.queue?.length < 1" style="margin-top: 25px; text-align: center">
            <label for="file-input" style="justify-content: center">
              <img src="../../../assets/plus.png" alt="Add Photo" class="image is-32x32" style="margin: auto"> Add Image
            </label>
            <input class="file-input" id="file-input" type="file" name="photo" ng2FileSelect [uploader]="uploader">
          </div>
          <div class="spinner" *ngIf="uploaderLoading"></div>
          <div class="content" style="color: #fdfdfd; font-size: 20px; text-align: center" *ngIf="!uploaderLoading">
            <span *ngIf="uploader?.queue?.length < 1" style="color: #111">No images uploaded</span>
            <span *ngFor="let item of uploader.queue" style="color: #111">{{item?.file?.name}}</span>
          </div>
        </section>
        <footer class="modal-card-foot">
          <button class="button is-success" (click)="onUploadClicked()">Save</button>
          <button class="button" (click)="onCloseModal()">Cancel</button>
        </footer>
      </div>
    </div>
  `,
  styleUrls: ['../transaction.component.css']
})
export class OwnerPostEvalComponent implements OnInit, DoCheck {
  ownerPostEvalActive = false;
  ownerPostEval: string;
  transaction: Transaction;
  file: any;
  uploaderLoading = false;
  devUrl = 'XXXXXX';
  url = 'XXXXX';
  public uploader:FileUploader = new FileUploader({url: this.devUrl, itemAlias: 'photo'});

  constructor(
    private router: Router,
    private uploadS: UploadService,
    private tranS: TransactionService,
    private uis: UiService) {}

  ngOnInit() {
    this.uploader.onAfterAddingFile = (file)=> { file.withCredentials = false; };

    this.uploader.onCompleteItem = (item:any, response:any, status:any, headers:any) => {
      let awsUrl = 'XXXXX' + response;
      this.onUpdateTransaction(awsUrl);
    };
  }

  ngDoCheck() {
    this.ownerPostEvalActive = this.uis.onReturnEval('ownerPost');
  }

  // pre eval
  onUpdateTransaction(fileName: string) {
    this.transaction = this.tranS.onReturnTransactionService();
    this.transaction.ownerPostReview = this.ownerPostEval;
    this.transaction.ownerPostFilled = true;
    this.transaction.ownerPostImages.push(fileName);
    this.tranS.onUpdateTransaction(this.transaction.id, this.transaction)
      .subscribe(update => {
        console.log(update);
        this.uploaderLoading = false;
        this.router.navigate(['/profile']);
        this.uis.onFlash('Post Evaluation Posted Successfully', 'success');
        this.onCloseModal();
      }, resp => {
        console.log(resp);
        this.uis.onFlash('Error Posting Post Evaluation', 'error');
      })
  }

  onUploadClicked() {
    this.uploader.uploadAll();
    this.uploaderLoading = true;
  }

  // UI

  styleObject(s: string): Object {
    if (s === 'ownerPost') {
      if (this.ownerPostEvalActive) {
        return {'height': '100%'};
      } else {
        return {'height': 0};
      }
    }
  }

  onCloseModal(){
    this.uis.onCloseModal('ownerPost');
  }



}

and here is the code for the component that is not working

import {Component, DoCheck, OnInit} from '@angular/core';
import {UiService} from "../../ui.service";
import {Transaction} from "../transaction.model";
import {TransactionService} from "../transaction.service";
import {FileUploader} from "ng2-file-upload";
import {UploadService} from "../../settings/account-settings/contracts/upload-service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-renter-post-eval',
  template: `
    <div class="modal is-active" [ngStyle]="styleObject('renterPost')">
      <div class="modal-background"></div>
      <div class="modal-card">
        <header class="modal-card-head">
          <p class="modal-card-title">Post Evaluation</p>
        </header>
        <section class="modal-card-body">
          <form *ngIf="!uploaderLoading">
            <label class="label">Post Evaluation</label>
            <textarea class="textarea"
                      [(ngModel)]="renterPostEval"
                      placeholder="Please leave a review of your items current state."
                      name="renterPostEval"></textarea>
          </form>
          <div class="field image-upload" *ngIf="uploader?.queue?.length < 1" style="margin-top: 25px; text-align: center">
            <label for="file-input" style="justify-content: center">
              <img src="../../../assets/plus.png" alt="Add Photo" class="image is-32x32" style="margin: auto"> Add Image
            </label>
            <input class="file-input" id="file-input" type="file" name="photo" ng2FileSelect [uploader]="uploader">
          </div>
          <div class="spinner" *ngIf="uploaderLoading"></div>
          <div class="content" style="color: #fdfdfd; font-size: 20px; text-align: center" *ngIf="!uploaderLoading">
            <span *ngIf="uploader?.queue?.length < 1" style="color: #111">No images uploaded</span>
            <span *ngFor="let item of uploader.queue" style="color: #111">{{item?.file?.name}}</span>
          </div>
        </section>
        <footer class="modal-card-foot">
          <button class="button is-success" (click)="onUploadClicked()">Save</button>
          <button class="button" (click)="onCloseModal()">Cancel</button>
        </footer>
      </div>
    </div>
  `,
  styleUrls: ['../transaction.component.css']
})
export class RenterPostEvalComponent implements OnInit, DoCheck {
  renterPostEvalActive = false;
  renterPostEval: string;
  transaction: Transaction;
  file: any;
  uploaderLoading = false;
  devUrl = 'XXXXX';
  url = 'XXXXX';
  public uploader:FileUploader = new FileUploader({url: this.devUrl, itemAlias: 'photo'});

  constructor(
    private router: Router,
    private uploadS: UploadService,
    private tranS: TransactionService,
    private uis: UiService) {}

  ngOnInit() {
    this.uploader.onAfterAddingFile = (file)=> { console.log(file); file.withCredentials = false; };

    this.uploader.onCompleteItem = (item:any, response:any, status:any, headers:any) => {
      let awsUrl = 'XXXX' + response;
      this.onUpdateTransaction(awsUrl);
    };
  }

  ngDoCheck() {
    this.renterPostEvalActive = this.uis.onReturnEval('renterPost');
  }

  // pre eval
  onUpdateTransaction(fileName: string) {
    this.transaction = this.tranS.onReturnTransactionService();
    this.transaction.renterPostReview = this.renterPostEval;
    this.transaction.renterPostFilled = true;
    this.transaction.renterPostImages.push(fileName);
    this.tranS.onUpdateTransaction(this.transaction.id, this.transaction)
      .subscribe(update => {
        console.log(update);
        this.uploaderLoading = false;
        this.router.navigate(['/profile']);
        this.uis.onFlash('Post Evaluation Posted Successfully', 'success');
        this.onCloseModal();
      }, resp => {
        console.log(resp);
        this.uis.onFlash('Error Posting Post Evaluation', 'error');
      })
  }

  onUploadClicked() {
    this.uploader.uploadAll();
    this.uploaderLoading = true;
  }

  // UI

  styleObject(s: string): Object {
    if (s === 'renterPost') {
      if (this.renterPostEvalActive) {
        return {'height': '100%'};
      } else {
        return {'height': 0};
      }
    }
  }

  onCloseModal(){
    this.uis.onCloseModal('renterPost');
  }



}

A note about these files, they both are used at the same time because they are modals. Here is the file they are called in as directives.

<!-- evaluation modals -->
<app-owner-post-eval></app-owner-post-eval>
<app-renter-post-eval></app-renter-post-eval>
<app-report-issue></app-report-issue>
<app-message-modal></app-message-modal>


<!-- Main container -->
<nav class="level">
  <!-- Left side -->
  <div class="level-item">
    <h3 class="title">Transaction</h3>
  </div>
</nav>

I've tried looking at the code and spotting any differences but it is not working. Can anyone help?

Jonathan Corrin
  • 1,219
  • 4
  • 17
  • 42
  • what is the error in your console. and how you are accessing the ng2 file upload plugin. where did you import the ng2 file upload app.module.ts or some other module? – Sathish Kotha Sep 11 '17 at 05:35
  • There is no error, after clicking a png or jpeg to upload, the upload screen fades and nothing happens. I've tried placing a console.log('test') in the onAfterUpload function, but it is never called. I am importing the fileUploadModule in my general appModule. I've yet to separate the application into multiple modules. Im accessing the upload plugin through my service of which has the fileUploader imported – Jonathan Corrin Sep 11 '17 at 16:39
  • I have tried in two componets with file upload same code, its working . I have no issues with uploader , I am using lazy load concept. I have imported ng2 file upload plugin in share.module.ts file. and that module imported by main app.module.ts file. so I am not getting any issue with file uploader. can you reproduce a plunker for better understand.? – Sathish Kotha Sep 11 '17 at 18:02
  • The files are actually called in the same component file. Im using the html attributes as modals as seen above. They are all active at the same time. Will that affect it? – Jonathan Corrin Sep 11 '17 at 19:28

0 Answers0