简体   繁体   English

在可观察数组中嵌套* ngFor

[英]Nested *ngFor in Observable Array

I am trying to bind a nested observable array using Angular 6 in my HTML and show the list(image_urls) in my carousel. 我试图在我的HTML中使用Angular 6绑定一个嵌套的可观察数组,并在轮播中显示list(image_urls)。 However, I am unable to bind the nested array. 但是,我无法绑定嵌套数组。 Any help or advice is much appreciated. 任何帮助或建议,我们将不胜感激。 Thanks in advance 提前致谢

This is what I have so far: 这是我到目前为止的内容:

Components.ts Components.ts

getAvailableCars()
{
    this.cars$ = this.carService.getAvailableCars();
         this.cars$.subscribe(car => {
            car.forEach(c => {
             c.image_urls = new Array<any>(); 
                this.fileUploads = this.uploadFileService.getFiles(c.car_id);
                this.fileUploads.subscribe(res => {
                 c.image_urls = res;
             });
        });
    });
   }

HTML HTML

<div class="row" *ngFor="let car of cars$ | async”>
    <span>{{car.name}}</span>

    <mdb-carousel [isControls]="true" [animation]="'slide'”> 
        <mdb-slide *ngFor="let file of car.image_urls | async”> 
            <span>{{file.url}}</span>
        </mdb-slide>
     </mdb-carousel>
</div>

Console 安慰

Car(3) [{…}, {…}, {…}]
0:
car_id: 30
exterior_color: 4
fuel_type_name: "Petrol"
image_urls: Array(2)
0: FileUpload {url: "https://s3.amazonaws.com/xx/30/SocialPost_5695408_facebook.png"}
1: FileUpload {url: "https://s3.amazonaws.com/xx/30/logo_size.jpg"}
length: 2
__proto__: Array(0) 

If i use this.fileUploads directly in the Carousel it works, but not when i push it to c.image_urls. 如果我直接在Carousel中使用this.fileUploads,则可以使用,但是当我将其推送到c.image_urls时却不能。 Since, I have multiple cars and each have different image urls, I need to add the image_urls to the object of car. 由于我有多辆汽车,每辆汽车都有不同的图像URL,因此我需要将image_urls添加到汽车的对象中。

Thanks again! 再次感谢!

I got it working. 我知道了 I was modifying the observable in subscribe instead of in map. 我在订阅中而不是在地图中修改了可观察对象。

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

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