简体   繁体   English

如何将 html 中迭代对象的值传递给 Angular2/4 中的组件?

[英]How do you pass values from iterated object in html to component in Angular2/4?

I am allowing file uploads to S3 in my Angular2/4 app.我允许在我的 Angular2/4 应用程序中将文件上传到 S3。 It is set up so that when a user uploads in file, it'll store on a S3 bucket.它被设置为当用户上传文件时,它将存储在 S3 存储桶中。 After uploading, it'll show the user the files uploaded.上传后,它会向用户显示上传的文件。 If they accidentally uploaded the wrong file, I'd like to allow the option to delete the image with button that'll delete the corresponding file on S3.如果他们不小心上传了错误的文件,我想允许删除带有按钮的图像的选项,该按钮将删除 S3 上的相应文件。 I have the key(needed to delete image on S3) but need help passing it to the component.我有密钥(需要在 S3 上删除图像)但需要帮助将其传递给组件。 I know using hidden inputs won't work.我知道使用隐藏输入是行不通的。 I'd assume passing the key might work, but there's a chance it'll break since the key ends with a file extension.我认为传递密钥可能会起作用,但由于密钥以文件扩展名结尾,因此它有可能会中断。 Any thoughts?有什么想法吗?

upload.component.html上传.component.html

<div *ngFor = "let file of files">
 <td>{{ file.originalname }}</td>
 <td><img src="{{ file.location }}"></td>
</div>

I need to pass {{ file.key }} to the component somehow via a delete button.我需要通过删除按钮以某种方式将 {{ file.key }} 传递给组件。

upload.component.ts上传.component.ts

import { Component, OnInit } from '@angular/core';
import { FileUploader } from 'ng2-file-upload';

const URL = 'http://localhost:6789/api/upload';

@Component({
  selector: 'app-upload',
  templateUrl: './upload.component.html',
  styleUrls: ['./upload.component.css']
})
export class UploadComponent implements OnInit {
  public uploader:FileUploader = new FileUploader({url: URL});
  public hasBaseDropZoneOver:boolean = false;
  public hasAnotherDropZoneOver:boolean = false;

  // THIS GETS POPULATED WITH OBJECTS FROM S3 AFTER UPLOAD
  public files:any;

  public files_path: any;

  public fileOverBase(e:any):void {
    this.hasBaseDropZoneOver = e;
  }

  constructor(
    public _uploadService: UploadService
  ) { }

  ngOnInit() {
    this.files = [];
    this.files_path = [];
    this.uploader.onCompleteItem = (item:any, response:any, status:any, headers:any) => {
      response = JSON.parse(response)
      this.files.push(response);
      this.files_path.push(response.location);
    }
  }

What about creating a button and pass the file.key to a function like so?创建一个按钮并将 file.key 传递给这样的函数怎么样?

<div *ngFor = "let file of files">
    <td>{{ file.originalname }}</td>
    <td><img src="{{ file.location }}"></td>
    <button (click)="deleteImage(file.key)">Delete</button>
</div>

And then you can have in your component the function:然后你可以在你的组件中使用函数:

deleteImage(fileKey) {
    //Call the service and pass the fileKey as a payload
}

you can use ether this:你可以使用以太:

 <div *ngFor = "let file of files">
     <td>{{ file.originalname }}</td>
     <td><img src="{{ file.location }}" (click)="delete(file)"></td>
    </div>

or this:或这个:

<div *ngFor = "let file of files;let i = index">
 <td>{{ file.originalname }}</td>
 <td><img src="{{ file.location }}" (click)="delete(i)"></td>
</div>

or this if file has the key inside:或者如果文件里面有密钥:

<div *ngFor = "let file of files">
     <td>{{ file.originalname }}</td>
     <td><img src="{{ file.location }}" (click)="delete(file.key)"></td>
    </div>

and then in delete do what ever you want然后在删除中做你想做的事

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

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