Skip to content

Commit a965589

Browse files
Issei HorieAndrewKushnir
authored andcommittedNov 19, 2020
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
1 parent ce604b9 commit a965589

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed
 

‎goldens/public-api/core/core.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ export declare class QueryList<T> implements Iterable<T> {
756756
filter(fn: (item: T, index: number, array: T[]) => boolean): T[];
757757
find(fn: (item: T, index: number, array: T[]) => boolean): T | undefined;
758758
forEach(fn: (item: T, index: number, array: T[]) => void): void;
759+
get(index: number): T | undefined;
759760
map<U>(fn: (item: T, index: number, array: T[]) => U): U[];
760761
notifyOnChanges(): void;
761762
reduce<U>(fn: (prevValue: U, curValue: T, curIndex: number, array: T[]) => U, init: U): U;

‎packages/core/src/linker/query_list.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ export class QueryList<T> implements Iterable<T> {
6363
if (!proto[symbol]) proto[symbol] = symbolIterator;
6464
}
6565

66+
/**
67+
* Returns the QueryList entry at `index`.
68+
*/
69+
get(index: number): T|undefined {
70+
return this._results[index];
71+
}
72+
6673
/**
6774
* See
6875
* [Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

‎packages/core/test/linker/query_list_spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testin
5151
expect(queryList.length).toEqual(2);
5252
});
5353

54+
it('should support get', () => {
55+
queryList.reset(['one', 'two']);
56+
expect(queryList.get(1)).toEqual('two');
57+
});
58+
5459
it('should support map', () => {
5560
queryList.reset(['one', 'two']);
5661
expect(queryList.map((x) => x)).toEqual(['one', 'two']);

0 commit comments

Comments
 (0)
Please sign in to comment.