如何在angular 6中的forEach中使用case语句?

时间:2019-05-08 14:43:16

标签: javascript angular typescript foreach angular6

我想从 typesFurniture 的app.html中选择产品,在uploadFiles()函数中执行我选择的产品,但是我无法使其工作。请帮帮我!

我已经尝试过forEach和case语句,但是没有用。不知道为什么...

当我通过下拉列表选择我的产品之一时,我想添加图像,我的产品属于数组类型家具。但是我的问题是我不知道从下拉列表中选择目标产品,然后粘贴此代码

const fileToUpload = this.files; 
const filesIdx = _.range(fileToUpload.length); 
_.each(filesIdx, (idx) => { 
this.upload = new Upload(fileToUpload[idx]); this.uploadService.uploadFile(this.upload);

app.ts

export class DodavanjeSlikaComponent {
  typesFurniture = ["Kuhinje", "Klizni ormari", "Ormari", "Radni stolovi", "Stolovi", "Cipelari", "TV Komode", "Police", "Paravani"];

  files: FileList;
  upload: Upload;

  constructor(private uploadService: UploadService) { }

  handleFiles(event) {
    this.files = event.target.files;
  }

  uploadFiles() {
    /* this have to include!
    const fileToUpload = this.files;
      const filesIdx = _.range(fileToUpload.length);
      _.each(filesIdx, (idx) => {
       this.upload = new Upload(fileToUpload[idx]);
       this.uploadService.uploadFile(this.upload);
 */

    // why this does not work, it won't execute the check that I've selected

    this.typesFurniture.forEach(i => {
      const fileToUpload = this.files;
      const filesIdx = _.range(fileToUpload.length);

      switch (i) {
        case "Kuhinje":
          _.each(filesIdx, (idx) => {
            this.upload = new Upload(fileToUpload[idx]);
            this.uploadService.uploadFile(this.upload);
          });
          break;

        case "Klizni ormari":
          _.each(filesIdx, (idx) => {
            this.upload = new Upload(fileToUpload[idx]);
            this.uploadService.uploadFile2(this.upload);
          });
          break;
      }
    })

  }

}

app.html

<div id="dodavanje-proizvoda">
  <div class="dodavanje-kocka">
    <div class="vrsta">
      <p>Izaberite vrstu proizvoda:</p>
      <select>
        <option *ngFor="let furniture of typesFurniture">{{furniture}}</option>
      </select>     
    </div>
    <div class="vrsta2">
      <input type="file" (change)="handleFiles($event)" multiple>
    </div>
    <div class="vrsta3">
      <input type="submit" value="Dodajte sliku" (click)="uploadFiles()">
    </div>
  </div>
</div>

0 个答案:

没有答案