Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryList<T>.get(index) #29467

Closed
FallenMireq opened this issue Mar 22, 2019 · 7 comments
Closed

QueryList<T>.get(index) #29467

FallenMireq opened this issue Mar 22, 2019 · 7 comments
Labels
area: core Issues related to the framework runtime core: queries design complexity: low-hanging feature Issue that requests a new feature
Milestone

Comments

@FallenMireq
Copy link

馃殌 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;
  }
}
@pkozlowski-opensource
Copy link
Member

Why not, but you can do queryList.toArray()[index] today

@pkozlowski-opensource pkozlowski-opensource added the area: core Issues related to the framework runtime label Mar 22, 2019
@ngbot ngbot bot added this to the needsTriage milestone Mar 22, 2019
@FallenMireq
Copy link
Author

@pkozlowski-opensource

But slicing array every time isn't a good solution, I guess..

QueryList.toArray():

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

  toArray(): T[] { return this._results.slice(); }

  // ...
}

@pkozlowski-opensource
Copy link
Member

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...

@mhevery mhevery added the feature Issue that requests a new feature label Apr 4, 2019
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Apr 4, 2019
@dannymcgee
Copy link

@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.

@pburkindine
Copy link

Also like to see this, just awkward boilerplate to pull the array every time

is2ei pushed a commit to is2ei/angular that referenced this issue May 4, 2020
This commit adds get method to QueryList.

PR Close angular#29467
is2ei pushed a commit to is2ei/angular that referenced this issue May 4, 2020
This commit adds get method to QueryList.
The method returns an item of the internal results.

PR Close angular#29467
is2ei pushed a commit to is2ei/angular that referenced this issue May 4, 2020
This commit adds get method to QueryList.
The method returns an item of the internal results by index number.

PR Close angular#29467
is2ei pushed a commit to is2ei/angular that referenced this issue May 4, 2020
This commit adds get method to QueryList.
The method returns an item of the internal results by index number.

PR Close angular#29467
@Merott
Copy link

Merott commented Jul 22, 2020

Currently accessing by index like this:

queryList.find((_e, i) => i === index);

But sure, it would be nice to just do:

queryList.get(index);

mhevery pushed a commit to is2ei/angular that referenced this issue Nov 17, 2020
This commit adds get method to QueryList.
The method returns an item of the internal results by index number.

PR Close angular#29467
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime core: queries design complexity: low-hanging feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

6 participants