Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
autocomplete taking time to render for first time

I have an input field where i am searching for locations through bing api.

this.addressForm
  .get('queryField')
  .valueChanges.debounceTime(100)
  .distinctUntilChanged()
  .switchMap(query => this.onChangeSearch(query))
  .subscribe(res => {
    console.log(res);
  });

Note:

When we load the app, for the First time when i enter only 1 character in the field, its taking couple of seconds to render the view. The API response is fine, but the list gets populated after couple of seconds.

This does not happen from second time onwards.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Change detection - here's the fork: https://stackblitz.com/edit/angular-ng-autocomplete-7ryhdv?file=src%2Fapp%2Fapp.component.ts

These are the lines:

  constructor(
    private bingApiLoader: BingApiLoaderService,
    private fg: FormBuilder,
    private ref: ChangeDetectorRef <- ADD THIS
  ) {}

...

    this.manager.getSuggestions(search, res => {
      this.countries = res.map(r => {
        return {
          formattedSuggestion: r.formattedSuggestion,
          address: r.address
        };
      });
      this.results = this.countries;
      this.ref.detectChanges(); <- AND THIS
    });

In reality, you should be using observables for everything (instead of instance members) and your template should just be subscribing to them.

Here's an example of what I mean. https://stackblitz.com/edit/angular-ivy-k47m7k

It uses a package I wrote a long time ago (ignore the package itself) but just pay attention to how there are no instance members and all the template does is subscribe to observables with async pipe.

I've seen a lot of very experienced angular devs do exactly what the OP has done regarding stateful components* - just create some instance members, call detectChanges() when they feel like they have to. But in reality, when you are dealing with a stateful component, your template should just be subscribing to observables.

*A stateful component is a component that has created some sort of data that it will pass on to child components.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!