如何从列表中的每个项目获取真实图像值并将其订阅到另一个列表?

我知道

我有一个服务列表,该服务具有多个属性,例如使用Spring REST API从数据库调用的serviceId,serviceName和photoProfile。

“ photoProfile”属性仅具有个人资料图片的ID,如果您使用“ localhost:8082 / downloadFile /” + photoProfile,则会获得图像,该图像又存储在spring项目的文件夹中。

在网上寻找了一会儿之后,我发现了如何真正地将真实图像显示在我的网站上,但是现在我陷入了困境,因为我需要对整个列表进行此操作。

这是我的角度代码:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { LoginComponent } from '../login/login.component';
import { UserService } from '../user.service';
import { Observable, forkJoin } from 'rxjs';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { combineLatest } from 'rxjs/operators';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
  loggedIn: boolean;
  services: any[] = [];
  imgSrc: any;
  newList: any[] = [];
  constructor(private router: Router, private service: UserService, private http: HttpClient) {
  }

  ngOnInit() {
    this.service.getServices().subscribe(res => {
    this.services = res;
    console.log('services: ', this.services);
});
    for (let i = 0; i < this.services.length; i++) {
      const element = this.services[i];
      this.getImage('http://localhost:4200/downloadFile/' + element.photoProfile).subscribe(data => {
      this.createImageFromBlob(data);
    });
    this.newList.push(this.imgSrc);
    console.log(this.newList);
    //I want to add the element from the services list and the image value after being converted to the new list
    }
  }

  getImage(imageUrl: string): Observable<Blob> {
    return this.http.get(imageUrl, {responseType: 'blob'});
  }

  createImageFromBlob(image: Blob) {
    const reader = new FileReader();
    reader.addEventListener('load', () => {
      this.imgSrc = reader.result;
    }, false);
    if (image) {
      reader.readAsDataURL(image);
    }
  }
}

感谢您的帮助。

穆罕默德·卡姆兰

订阅服务列表后,需要在ngOnInit中添加新列表。因为目前。for循环运行时,您没有服务。从服务获得结果后,需要运行for循环。像这样:

ngOnInit() {
    this.service.getServices().subscribe(res => {
    this.services = res;
    console.log('services: ', this.services);

    for (let i = 0; i < this.services.length; i++) {
      const element = this.services[i];
      this.getImage('http://localhost:4200/downloadFile/' + element.photoProfile).subscribe(data => {
        this.createImageFromBlob(data);
        element.imgSrc = this.imgSrc;
        this.newList.push(element);
       });
    console.log(this.newList);
    }
  }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从下拉列表中获取值并将该值自动传递到另一个表单?

如何比较列表中的某个字段并将其值插入另一个列表中?

单击React元素列表并将其作为值存储在另一个列表中

如何从开始到另一个列表的特定项目获取项目列表C#

如何从另一个表中获取具有最旧值的项目列表

如何从列表视图获取数据并将其传递给另一个活动

Java 8如何处理一个列表中的对象并将其收集到另一个列表中?

如何从另一个列表中的列表中获取另一个列表中的值

如何从一个列表中获取一个值并将其应用于另一列表中的所有值

如何从列表中的学生获取 id 并将其显示在另一个页面上的学生信息?Ruby on Rails

如何从列表中提取索引值并将其替换在另一个特定的数据帧R中?

从某个元素拆分一个列表,并将项目值放入Python 2.7中的另一个列表

如何获得每个矩阵行的最大值并将它们保存在另一个列表中?

如何从 Excel 中的列表中查找值并将这些文件复制到另一个目录?

如何扫描数组中的每个值并将其与另一个数组中的每个值进行比较?

获取列表的内容并将其附加到另一个列表

如何从两个单独的列表中获取公共整数元素的索引并将其插入另一个列表?

Python:如何连续删除列表的最小值并将其添加到另一个列表?

如何从列表中随机提取并将其放入 Python 中的另一个列表中?

如何创建一个列表,其中包含对 Haskell 中另一个列表的每个项目的计算?

如何获取特定列表项并将两个值从一个活动传递到另一个活动?

当每个项目也包装在另一个元组中时,如何从元组列表中获取第一个项目

从另一个列表中的列表中的每个列表中提取相同的项目

如何将一个列表的2个项目分组到另一个列表中

如何从具有子列表的列表中获取值并将所有值按顺序存储在另一个列表中

如何使用 lodash 扩展对象列表并将其与另一个列表合并

您如何从列表中提取随机数并将其放在另一个列表中

打开JSON文件列表并将其存储在另一个列表中

如何在列表视图中点击该项目并将其发送到另一个页面