feat(core): adds get method to QueryList (#36907)

This commit adds get method to QueryList.
The method returns an item of the internal results by index number.

PR Close #29467

PR Close #36907
This commit is contained in:
Issei Horie 2020-05-04 02:04:56 +09:00 committed by Andrew Kushnir
parent ce604b909b
commit a965589eb8
3 changed files with 13 additions and 0 deletions

View File

@ -756,6 +756,7 @@ export declare class QueryList<T> implements Iterable<T> {
filter(fn: (item: T, index: number, array: T[]) => boolean): T[];
find(fn: (item: T, index: number, array: T[]) => boolean): T | undefined;
forEach(fn: (item: T, index: number, array: T[]) => void): void;
get(index: number): T | undefined;
map<U>(fn: (item: T, index: number, array: T[]) => U): U[];
notifyOnChanges(): void;
reduce<U>(fn: (prevValue: U, curValue: T, curIndex: number, array: T[]) => U, init: U): U;

View File

@ -63,6 +63,13 @@ export class QueryList<T> implements Iterable<T> {
if (!proto[symbol]) proto[symbol] = symbolIterator;
}
/**
* Returns the QueryList entry at `index`.
*/
get(index: number): T|undefined {
return this._results[index];
}
/**
* See
* [Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

View File

@ -51,6 +51,11 @@ import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testin
expect(queryList.length).toEqual(2);
});
it('should support get', () => {
queryList.reset(['one', 'two']);
expect(queryList.get(1)).toEqual('two');
});
it('should support map', () => {
queryList.reset(['one', 'two']);
expect(queryList.map((x) => x)).toEqual(['one', 'two']);