簡體   English   中英

如何在 angular typescript 中自動增加參數的 ID?

[英]How to auto increment the id of an parameter in angular typescript?

我需要使用@ks89/angular-modal-gallery創建一個畫廊,剩下要做的就是為畫廊提供不同的ID。 我嘗試使用 for 循環,但它給了我未定義的..

在此處輸入圖像描述

page1.ts

getAllPosts() {
    this.authService.getAllPosts().pipe(takeUntil(this.destroy$)).subscribe((res: any) => {

      const ress = res;
      console.log(ress);

      this.getFullPosts = res.data.post;
      this.personSubject.next(res.data.post)

      this.userGallery = res.data.post.forEach(img => {
        img.galleryImages = img.images.map(image => {

          console.log(image.lenght)
          return new Image(
           id:  //here i have to pass the id for each gallery,
            {
              img: image,
              extUrl: image,
              title: image,

            })



        });
      });

    
    });

  }

圖片.ts

export declare class Image {
    id: number;
    modal: ModalImage;
    plain?: PlainImage;
    constructor(id: number, modal: ModalImage, plain?: PlainImage);
}

當您使用 map 或 forEach 或查找數組時,您可以使用附加參數,索引(第一個是 0)

img.galleryImages = img.images.map(
    (image:any,index:number)=>new Image({id:index,...image})
)

但是,真的檢查您的畫廊中是否需要“id”。 在 Angular 通常在 *ngFor 中不需要有不同的 id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM