Perform an $http.get request inside an event listener in Angular2 / Ionic2

In my attempt to dynamically fetch data on scroll and populate an array of "Article[]".

httpLoadArticles() {
    this.httpWSGetInit().subscribe(
                                   articles => this.articles = articles,
                                    err => {
                                        console.log(err);
                                    },
                    () => console.log('Search complete')
}

Presenting the current function below.

httpWSGetInit() : Observable<Article []> {

    return this.http.get(R.WEBSITE_URL_WS + this.articlesPagination)
                         .map((res:Response) => res.json())
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}

The functionality works effectively.

However, the http.get method is not triggered within addScrollListener.

@ViewChild(Content) content: Content;

ionViewDidLoad() {
        this.content.addScrollListener(this.onPageScroll);
}

onPageScroll(event) {
       this.httpLoadArticles()
}

I attempted to make the GET request synchronous, but it appears to have no effect. Could this be a scope-related issue? For instance, does addScrollListener function solely as a JavaScript method and cannot be linked to an Angular2 container?

Answer №1

It appears to be a scope problem. Utilizing an Arrow function can help maintain the correct context

ionViewDidLoad() {
    this.content.addScrollListener(() => this.onPageScroll() );
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to efficiently verify if a URL is valid by checking if it loads a page with content

INQUIRY: How can I verify if a URL is valid and loads a webpage successfully? Currently, my code only checks the status code, which means that a URL like will be considered valid even though it does not load any content. Is there a way to ensure that t ...

What external libraries does Angular 4 utilize during execution, aside from RxJS?

Angular 4 relies on RxJS types in its public API and also internally depends on RxJS. It would be beneficial to explore if Angular utilizes other external packages for certain functionalities, allowing us to incorporate them into our own projects. This ap ...

Mastering the art of debugging a mongoose action in node.js

I am utilizing mongoose for connecting my node.js app with mongoDB. However, I am facing an issue where the database does not get updated when I create or update a model instance. How can I effectively debug and identify what goes wrong in the create or up ...

Is the webdriver.io waituntil method designed to return a boolean value of true or false

I am working on an automation framework using webdriver.io v5. I need to receive a boolean response from the code snippet below: waitAndCheckForContactToBePresent(contactName) { return browser.waitUntil((value) => { return this.chec ...

The initial load of Angular BehaviorSubject is causing some issues

I have set up a BehaviorSubject in the file profile.service.ts, with an initial value of false. I am subscribing to this BehaviorSubject from the component profileDialog.component.ts in the ngOnInit function, and updating the value from false to true in th ...

Oops, it seems like there was an issue with NextJS 13 Error. The createContext functionality can only be used in Client Components. To resolve this, simply add the "use client" directive at the

**Issue: The error states that createContext only works in Client Components and suggests adding the "use client" directive at the top of the file to resolve it. Can you explain why this error is occurring? // layout.tsx import Layout from "./componen ...

Searching for client using mqtt.js in Angular2 with Typescript yields no results

I am facing a unique issue while trying to incorporate the mqtt.js library into Angular 2 using TypeScript. Below is my app.component.ts file: import { Component } from '@angular/core'; import * as mqtt from 'mqtt'; @Component({ sel ...

the window.matchMedia event listening for the dark color scheme preference fires just a single time

My goal is to automatically change the theme mode (light/dark) based on the user's device preference using addEventListener. However, I am facing an issue where the event is only triggered once. I am currently working with Ionic and Angular. Angular ...

Utilizing Dynamic Class Names in Angular 7 with Typescript

In the process of developing an Angular 7 application, I have created a form component that is intended to be used for various entities. As part of this, I defined a variable route: {path: ':entity/create', component: FormComponent} While this ...

Confirm the presence of an element with the specified class; if not found, apply the class to the first element

Can you show me how to achieve the following: Determine if there are any elements in a set with a specific class. If not found, add the class to the first element in the set. Here is the code I have come up with: It seems to work well, but I'm wond ...

html issue with d3.js rendering images: works in firefox, but not in chrome

I am currently experiencing an issue with a D3 graphic that runs successfully in Firefox but fails to render in Chrome when using an HTML and JSON file. Upon checking the console, I see the following error message: d3.v3.min.js:1 Access to XMLHttpReque ...

The function setSize() is ineffective for adjusting the browser dimensions when using JavaScript in Selenium

Attempting to adjust the size of the browser window using the following JavaScript code: driver.get(url); driver.manage().window().setSize(1200,800); Encountering an error message: 'setSize is not a function\n'. Seeking assistance in r ...

Discovering the process of extracting a date from the Date Picker component and displaying it in a table using Material-UI in ReactJS

I am using the Date Picker component from Material-UI for React JS to display selected dates in a table. However, I am encountering an error when trying to show the date object in a table row. Can anyone provide guidance on how to achieve this? import ...

I'm curious why I need to add an extra await in my Vue.js unit test whenever multiple calls are made to my Firebase auth mock

In my vue.js application with firebase authentication, I encountered an issue while testing the Login.vue component. To mock firebase auth, I had to simulate the configuration file used for initializing firebase (named firebase.config). It's worth men ...

Exploring Recursive Methods in JavaScript with Function Declarations

Recursion with function expressions raises many questions. There are two approaches to accomplishing this: one involves using a named function expression, while the other employs the arguments.callee method. However, it's important to note that the us ...

Jquery Ajax is met with a 400 error indicating a BAD request

I've encountered an issue while trying to send data to my local DB server. Every time I attempt to send the data, I keep receiving a 400 Bad Request error. var studentEmail = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail ...

Triggering an exception for numerous entries in a JSON dataset

When working with my JSON data, I encountered an issue where multiple records were present, but only one record was being displayed in the browser. I attempted to use NgFor to repeat the data, but unfortunately, I encountered an error. Below is the code s ...

Update the content of <p> depending on the selections made in two <select> elements

I am facing a challenge where I need to modify a price by altering the choices in two <select> tags. For instance: If a user selects Silver 1 in the first <select> and Silver 3 in the second <select>, then the resulting <p>value&l ...

Angular's hashlocationstrategy comes in handy when sharing links on platforms like LinkedIn, as it allows for

Utilizing Angular with HashLocationStrategy: By default, my website link is If I wish to share a sub-route from my website on LinkedIn such as , the link appears as #/123 https://i.sstatic.net/Zyj7n.png I have attempted to remove the # symbol, but Ang ...

ERROR: The property "width" cannot be read from a null value in custom.js line 6

When attempting to center the image, an error is displayed in the browser stating "cannot read property width of null." I have tried dividing the screen width by 2 but I am unable to understand why this error is occurring. var mobile = document.getElem ...