question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QueryList<T>.get(index)

See original GitHub issue

πŸš€ feature request

Relevant Package

This feature request is for @angular/core

Description

Now there is no way to get element from QueryList<T> by index.

Describe the solution you’d like

Add method get(index) to QueryList

export class QueryList<T> {
  // ...

  get(index: number): T|undefined {
    return this._results[index];
  }

  // ...
}

Describe alternatives you’ve considered

Very expensive workaround:

function getQueryListItemByIndex<T>(list: QueryList<T>, index: number): T|undefined {
  let value;

  let found = list.some((x, i) => {
    if (i === index) {
      value = x;
      return true;
    }
    return false;
  });

  if (found) {
    return value;
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
dannymcgeecommented, Sep 29, 2019

@pkozlowski-opensource Even if not a performance bottleneck, it just seems like an oversight that the Angular team provided all of these array-mirroring methods like forEach and map for interacting with QueryLists, but didn’t provide a method for accessing an item by index. I agree with OP, this would be a great feature to have.

2reactions
pkozlowski-opensourcecommented, Mar 22, 2019

But slicing array every time isn’t a good solution

Sure, if you see it as a performance bottleneck in your application after perf-measuring it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular how to get index from viewChildren queryList items?
I've made a demo in which everything works (looping through viewChildren and getting the index). Try to compare this, maybe you've made aΒ ......
Read more >
QueryList
Returns the QueryList entry at index . get(index: number):Β ...
Read more >
Use ViewChildren to get the QueryList of elements or ...
Use ViewChildren to get the QueryList of elements or directives from the view DOM. In many cases you have used the ViewChild decorator...
Read more >
Angular how to get index from viewChildren queryList items?
I've made a demo in which everything works (looping through viewChildren and getting the index). Try to compare this, maybe you've made a...
Read more >
Angular - Understanding How To Use QueryList Properly.
Use to get the QueryList of elements or directives from the view ... Iterator<T> get(index: number): T | undefined map<U>(fn: (item: T,Β ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found