Troubleshooting Problem with HTTP Requests in Angular 2 on Firefox Browser

I have encountered a peculiar issue with my Angular 2 application specifically on Firefox and all iOS browsers (Firefox, Safari).

Whenever a user navigates to the /reports route in my application, I am making a REST API call using the ngOnInit method to fetch JSON data.

This snippet shows how I handle the data loading in my ngOnInit method:

ngOnInit() {
//this.service is my service for fetching reports
    this.service.getReports(this.categoryId).subscribe(res => {
        this.testReports = res;
    }
}

After receiving the response, I render the reports in the HTML file:

<div class="cat-item-holder" *ngFor="let singleCategory of testReports; let i=index">
{{singleCategory.name}}
</div>

In Chrome, all the reports are immediately visible upon entering the /reports page. However, on Firefox and Safari, the application seems to 'hang' and requires multiple clicks on the page for the API results to show up.

Is there a solution to fix this issue? Could it be related to the ngOnInit method?


Here is an excerpt from my angular-cli.json file:

{
  "project": {
    "version": "1.0.0-beta.22-1",
    "name": "APPNAME"
  },
  ...
}

-- My package.json file:

{
  "name": "front",
  "version": "0.0.0",
  ...
}

Lastly, here is how I implemented my reports service:

import {Injectable } from '@angular/core';
...
export class ReportsService {
   constructor(private http:Http , private mainService:MainService) {
    this.http = http;
  }

  getReports(categoryId) : Observable<Response> {
    ...
  }
}

While viewing my reports page on Firefox, I noticed a small scroll in the upper right corner of the loading section. Clicking on this scroll a few times properly renders the reports in Firefox. Could this be a DOM-related issue?


Update: I found a temporary workaround by adding some code to app.component.ts and reports.ts:

import {ApplicationRef} from '@angular/core';

export class AppComponent {

    constructor(private _applicationRef: ApplicationRef, private _router: Router) {
        setInterval(() => {
              this._applicationRef.tick();
           }, 5000);
    }

}
constructor(private ref: ChangeDetectorRef) {
        setInterval(() => {
           this.ref.detectChanges();
         }, 5000);
}

Now, after 5 seconds of the page load in Firefox, there is a slight hang-up of the browser, but the results are successfully rendered. Is there a way to prevent this hang-up?

Answer №1

Presented here as an 'answer', although it resembles more of a workaround stemming from your own creative solution. My belief is that the issue stems from the interaction between polymer and angular's change detection mechanism.

@Component({...})
export class ReportsComponent {

    constructor(private changeDetector: ChangeDetectorRef){}

    ngOnInit() {
        this.service.getReports(this.categoryId).subscribe(res => {
            this.testReports = res;
            setTimeout(() => {
                this.changeDetector.detectChanges();
            });
        }
    }
}

It's uncertain if utilizing setTimeout will be necessary, but that exploration is up to you. This action triggers a change detection on both this component and its child components. Essentially, the actions you take with mouse clicks and movements are also prompting angular's change detection process.

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

The React useState Props error message TS2322: Cannot assign type 'string' to type 'number'

I'm attempting to pass Props to React useState Hooks. Both of my props are required and should be numbers, but I keep receiving a Typescript error stating: Type 'string' is not assignable to type 'number'. TS2322 However, I am ...

Creating dynamic HTML with Angular 2 using property binding

After retrieving an HTML string from the database, I came across html='<span>{{name}}</span>' where 'name' is a component property. I am looking to display this string in HTML while maintaining the name binding. I have expl ...

Validation of input type number restricted to only numeric values

Is there a way to validate an input field with type="number" using only Reactive Forms in Angular, ensuring that the value is either numeric or null without utilizing directives? Only numbers [0-9] and . are permitted, no other characters. My Approach: ...

Tips for importing several makeStyles in tss-react

When upgrading from MUI4 to MUI5 using tss-react, we encountered a problem with multiple styles imports in some files. const { classes } = GridStyles(); const { classes } = IntakeTableStyles(); const { classes } = CommonThemeStyles(); This resulted in ...

React with Typescript prop is functioning well, despite displaying an error

After creating a component and passing props to styled components, everything seems to be working fine. However, I keep encountering this error on the first prop. Component type WelcomeProps = { image: String, color: String } const StyledWelcom ...

Mastering the art of mocking Rxjs Subject in an Angular application

I've set up a centralized DataStore to manage my report connections, handling events like onShow, onError, and onCancel so that the implementer doesn't need to worry about it. Now, I'm trying to figure out how to mock the SomeService.doSomet ...

Tips on specifying the data type of the second parameter in a TypeScript assignment operation

interface Individual { name: string, age: number, height: number, } const alice: Individual = { name: 'alice', age: 30, height: 160 } // Is there a way to specify the type of the second parameter (details) without resorting to &apos ...

Is it possible to alter the meaning of a word using the ngIf condition?

As a newcomer to Angular and Ionic, I am experimenting with retrieving JSON data from a URL and translating the words received to another language. My initial attempt using ngif did not yield the desired result! This is what I tried to do in order to chan ...

Securely getting a data point from a pathway

Within my Angular project, I recently made the transition from using query string elements in URLs such as: http://www.whatever.com/products?productName=TheMainProduct&id=234234 To a route-based system like this: http://www.whatever.com/products/The ...

Troubleshooting Next.js Mobile Freeze Issue: Unresponsive Scroll After Page Transition

Encountered a strange bug while testing my Next.js + Bootstrap demo project on mobile view. When using the burger menu to navigate to a new page on a mobile phone, attempting to scroll down causes it to stick/freeze/hang inexplicably. Despite my efforts to ...

What is the best method to compare two times and determine if they fall on the same date within an Angular 2/4 application? The time should be in the format of "HH:mm AM

Is there a way to validate if my time period falls on the same date? let startTime = currentSelection.startTimeHr + ":" + currentSelection.startTimeMin + " " + currentSelection.startTimeAMPM; let endTime = currentSelection.stopTimeHr + ":" + currentSele ...

Angular encountered an ERROR of type TypeError where it cannot access properties that are undefined when trying to read the 'title'

I designed a form and I am trying to save the information entered. However, when I attempt to use the save method, an error keeps popping up. How can I troubleshoot this issue and successfully save the data from the form? ...

The componentWillReceiveProps method is not triggered when a property is removed from an object prop

Just starting out with React, so I could really use some assistance from the community! I am working with a prop called 'sampleProp' which is defined as follows: sampleProp = {key:0, value:[]} When I click a button, I am trying to remo ...

Issue encountered while executing jest tests - unable to read runtime.json file

I've written multiple unit tests, and they all seem to pass except for one specific spec file that is causing the following error: Test suite failed to run The configuration file /Users/dfaizulaev/Documents/projectname/config/runtime.json cannot be r ...

Contrast between utilizing and publishing, demanding and bringing in within Express

I have recently started learning Typescript and Express. I have a simple exported function that looks like this: export function testFunction(req: any, res: any) { console.log(req.body); return res.status(200).send('OK'); }; And ...

Turn off Inline CSS in Angular Universal

While rendering a site on the server with Angular Universal, the resulting page looks great. However, the source code of the pages contains an excessive amount of inline CSS (more than 50% of the document > 500kb), leading to slow download times especia ...

Reinitializing various states using React Redux

My application consists of multiple Steps, each with their own state. I am attempting to create a link that triggers an onClick Action to reset all states back to their initial values. However, I am facing difficulties in achieving this. Within my Nav, I ...

Using RxJs in Angular, you can invoke an observable from within an error callback

I have a scenario where two observable calls are dependent on each other. Everything works fine, but when an error occurs in the response, I need to trigger another observable to rollback the transaction. Below is my code: return this.myService.createOrde ...

The issue of data being duplicated when clicking on react-cookie-consent

Currently, I am utilizing the npm package https://www.npmjs.com/package/react-cookie-consent to implement a cookies consent feature in my react TypeScript application. However, I have encountered two issues with this package. Firstly, upon clicking the acc ...

Struggling to get my React Typescript styled component slider to function within the component

Check out my demo here I created a simple react application using TypeScript and styled components. The main feature is a slider that adjusts the height of a red box. The red box is a styled component and I pass the height as a prop. Everything was fun ...