Ng-lazyload-image: Doesn't work with angular 5

Created on 9 Nov 2017  ·  4Comments  ·  Source: tjoskar/ng-lazyload-image

Hi, Ive tried this lib in angular 5 but couldnt seem to get it to work. I have a component template like so

<a class="card clickable" [routerLink]="[product.id]">
    <div class="card-img">
      <img [lazyLoad]="product.logoUrl">
    </div>
  <div class="card-block">
    <span class="card-media-title">{{product.name}}</span>
    <div class="product-version">{{product.version}}</div>
    <div class="product-release-date">{{product.releaseDate}}</div>
  </div>
</a>

and a component like so

import {Component, Input} from '@angular/core';
import {Product} from '../products.state';

@Component({
  selector: 'app-product-card',
  templateUrl: './product-card.component.html',
  styleUrls: ['./product-card.component.scss']
})
export class ProductCardComponent {

  @Input() product: Product;

  constructor() {
  }

}

And my module

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {LazyLoadImageModule} from 'ng-lazyload-image';
import {ProductsComponent} from './products.component';
import {ProductsEpic} from './products.epic';
import {ProductsListComponent} from './products-list/products-list.component';
import {ProductCardComponent} from './product-card/product-card.component';
import {ProductsService} from './products.service';
import {ProductsActions} from './products.actions';
import {ProductsEmptyComponent} from './products-empty/products-empty.component';
import {CoreModule} from '../core/core.module';
import {BannerComponent} from './banner/banner.component';
import {RouterModule} from '@angular/router';

@NgModule({
  imports: [
    CommonModule,
    RouterModule,
    CoreModule,
    LazyLoadImageModule
  ],
  declarations: [
    ProductsComponent,
    ProductsListComponent,
    ProductCardComponent,
    ProductsEmptyComponent,
    BannerComponent
  ],
  providers: [
    ProductsService,
    ProductsEpic,
    ProductsActions
  ]
})
export class ProductsModule {
}

The image simply never shows up. I also get this warning message in the console

image

Not sure what i'm doing wrong. Any help appreciated

Most helpful comment

My apologies. I looked closer into my issue and it turned out I had this piece of css which seemed to be disabling all scroll events in the browser

html, body {
  overflow-x: hidden;
}

To get around this I added this custom scroll event like so

import {Component, Input} from '@angular/core';
import {Product} from '../products.state';
import {Observable} from 'rxjs/Observable';

@Component({
  selector: 'app-product-card',
  templateUrl: './product-card.component.html',
  styleUrls: ['./product-card.component.scss']
})
export class ProductCardComponent {

  @Input() product: Product;
  scrollObservable = Observable.fromEvent(document.body, 'scroll');

  constructor() {
  }

}

and in my template like so

<a class="card clickable" [routerLink]="[product.id]">
  <div class="card-img">
    <img [lazyLoad]="product.logoUrl" [scrollObservable]="scrollObservable">
  </div>
  <div class="card-block">
    <span class="card-media-title">{{product.name}}</span>
    <div class="product-version">{{product.version}}</div>
    <div class="product-release-date">{{product.releaseDate}}</div>
  </div>
</a>

Now it works perfectly in angular 5

All 4 comments

Hi @el-davo,

Thanks for the report. Are you using any server rendering? Or precompile your app? Or are you running the app in develop mode?

Hi. Nope am not doing server side rendering. Yep currently in dev mode using the angular-cli

What browser are you using?
I just created a new angular app with the cli (Angular 5) and was not able to reproduce this. I did the following changes:

diff --git a/src/app/app.component.html b/src/app/app.component.html
index 46d517b..215f0c9 100644
--- a/src/app/app.component.html
+++ b/src/app/app.component.html
@@ -6,15 +6,21 @@
   <img width="300" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
 </div>
 <h2>Here are some links to help you start: </h2>
+<img [defaultImage]="defaultImage" [lazyLoad]="image">
 <ul>
   <li>
     <h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 7b0f672..cf8cead 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -7,4 +7,6 @@ import { Component } from '@angular/core';
 })
 export class AppComponent {
   title = 'app';
+  defaultImage = 'https://www.placecage.com/1000/1000';
+  image = 'https://images.unsplash.com/photo-1443890923422-7819ed4101c0?fm=jpg';
 }
diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index 926975a..7fb6d9d 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -1,18 +1,13 @@
 import { BrowserModule } from '@angular/platform-browser';
 import { NgModule } from '@angular/core';
+import { LazyLoadImageModule } from 'ng-lazyload-image';
 import { AppComponent } from './app.component';

 @NgModule({
   declarations: [
     AppComponent
   ],
   imports: [
    BrowserModule,
+   LazyLoadImageModule
   ],
   providers: [],
   bootstrap: [AppComponent]
 })
 export class AppModule { }

I also created a plnkr (https://embed.plnkr.co/rr046QU9YYWALdX7Xzf6/) but could not reproduce the error.
Do you think you could create a small git repo or a plnkr to reproduce the error?

My apologies. I looked closer into my issue and it turned out I had this piece of css which seemed to be disabling all scroll events in the browser

html, body {
  overflow-x: hidden;
}

To get around this I added this custom scroll event like so

import {Component, Input} from '@angular/core';
import {Product} from '../products.state';
import {Observable} from 'rxjs/Observable';

@Component({
  selector: 'app-product-card',
  templateUrl: './product-card.component.html',
  styleUrls: ['./product-card.component.scss']
})
export class ProductCardComponent {

  @Input() product: Product;
  scrollObservable = Observable.fromEvent(document.body, 'scroll');

  constructor() {
  }

}

and in my template like so

<a class="card clickable" [routerLink]="[product.id]">
  <div class="card-img">
    <img [lazyLoad]="product.logoUrl" [scrollObservable]="scrollObservable">
  </div>
  <div class="card-block">
    <span class="card-media-title">{{product.name}}</span>
    <div class="product-version">{{product.version}}</div>
    <div class="product-release-date">{{product.releaseDate}}</div>
  </div>
</a>

Now it works perfectly in angular 5

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vugar005 picture vugar005  ·  10Comments

kodeine picture kodeine  ·  7Comments

LobeTia picture LobeTia  ·  6Comments

walfro picture walfro  ·  11Comments

alisahinozcelik picture alisahinozcelik  ·  4Comments