ngx-gallery不显示图像

时间:2019-06-29 16:41:51

标签: angular7

Angular 7 ngx-gallery

我能够毫无错误地获取json格式的数据,但是ngx-gallery似乎没有,它已加载到页面详细信息中的元素中,但是图像和ngx-gallery模板未出现, 它可能不是从服务中拍照,但并没有记错, 这是我的代码:

import {
  NgxGalleryOptions,
  NgxGalleryImage,
  NgxGalleryAnimation
} from "ngx-gallery";

@Component({
  selector: "app-city-detail",
  templateUrl: "./city-detail.component.html",
  styleUrls: ["./city-detail.component.css"],
  providers: [CityService]
})
export class CityDetailComponent implements OnInit {

  constructor(
    private activatedRoute: ActivatedRoute,
    private cityService: CityService
  ) {}

  city: City;
  photos: Photo[] = []
  galleryOptions: NgxGalleryOptions[];
  galleryImages: NgxGalleryImage[];

  ngOnInit() {
    this.activatedRoute.params.subscribe(params => {
      this.getCityById(params["cityId"]);
    });
  }

  getCityById(cityId) {
    this.cityService.getCityById(cityId).subscribe(data => {
      this.city = data;
      this.getPhotosByCity(cityId)
    });
  }

  getPhotosByCity(cityId){
    this.cityService.getPhotosByCity(cityId).subscribe(data=>{

      this.photos = data;
      this.setGallery();
    })
  }

  getImages(){
    const imageUrls= []
    for(let i = 0;i<this.city.photos.length;i++){
      imageUrls.push({
        small:this.city.photos[i].url,
        medium:this.city.photos[i].url,
        big:this.city.photos[i].url
      })
    }
    return imageUrls;
  }

  setGallery(){
    this.galleryOptions = [
      {
          width: '100%',
          height: '400px',
          thumbnailsColumns: 4,
          imageAnimation: NgxGalleryAnimation.Slide
      },
      // max-width 800
      {
          breakpoint: 800,
          width: '100%',
          height: '600px',
          imagePercent: 80,
          thumbnailsPercent: 20,
          thumbnailsMargin: 20,
          thumbnailMargin: 20
      },
      // max-width 400
      {
          breakpoint: 400,
          preview: false
      }
  ];

  this.galleryImages = this.getImages()
  }
 }

模板:

<ngx-gallery *ngIf="galleryImages" [options]="galleryOptions" [images]="galleryImages"></ngx-gallery>

1 个答案:

答案 0 :(得分:-1)

本教程已完成

https://github.com/lukasz-galka/ngx-gallery-demo/blob/master/src/app/app.component.ts 使用方法 addImage 解决问题

help this image

 private loadAll() {
    this.imageService.getAllImage).subscribe(result => {
        this.addImage(result);
            });
        }


addImage(images: any[]): void
   images.forEach(image=>{  
        this.onlyPreviewExample.images.push(this.getImage(this.getRandomInt(1,2),doc));
      });
    }



private getImage(index: number, image?: any): NgxGalleryImage {
        return {
            big: 'imageUrl'
        }
    }


 private getImages(): NgxGalleryImage[] {
        let images = new Array<NgxGalleryImage>();
        return images;
    }


 private getRandomInt(min: number, max: number) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }


ngOnInit() {
        this.ngxGalleryModel = new Array<NgxGalleryModel>();
        this.ngxGalleryModel.push(
        this.onlyPreviewExample = new NgxGalleryModel('Only preview', this.getImages(), [{
            image: false,
            thumbnails: false,
            previewCloseOnClick: true,
            previewCloseOnEsc: true,
            previewZoom: true,
            previewRotate: true,
           // previewDownload: true,
            previewFullscreen: true,
            previewKeyboardNavigation: true,
            arrowNextIcon: "fa fa-arrow-left",
            arrowPrevIcon: "fa fa-arrow-right",
            fullscreenIcon: "fa fa-arrows-alt",
            width: '0px',
            height: '0px'
        }])
        )
    }


 @ViewChild('onlyPreviewGallery', {static: false}) onlyPreviewGallery: NgxGalleryComponent;
    ngxGalleryModel: NgxGalleryModel[];
    onlyPreviewExample: NgxGalleryModel;