I tried to lazy load images using Intersection Observable in my angular 7 application. I called Intersection Observable from ngAfterViewInit lifecycle hook. But when i load the application, callback is called twice, once with all the isIntersecting values as true and the next one with the correct isIntersecting values. I am confused with this behaviour.
ngAfterViewInit() {
const imgOne = document.querySelectorAll('[data-class]');
const options = {
// root: document
// threshold: 0
};
const observer = new IntersectionObserver((entries, observer) => {
console.log(entries.length);
entries.forEach((entry) => {
// console.log(entry);
if (!entry.isIntersecting) {
return;
} else {
const el = entry.target;
if (el.id === ('test' + this.testCount)) {
console.log(entry);
}
// observer.unobserve(entry.target);
}
});
});
imgOne.forEach((eachQuery) => {
observer.observe(eachQuery);
});
}
https://stackblitz.com/edit/angular-wqbuyr
UPDATE : Now intersection observer is called perfectly, but now a issue occured. Since we are using document.querySelectorAll which returns a static node list, once the array is updted node list is not updated. If i try to use document.getElementsByClassName, an error throws as it is not an Node list ???