1

I follow these to implement ViewChildren, but am unable to get the children. Why?

@ViewChildren(TextField) inputs: QueryList<TextField>;

ngAfterViewInit(): void {
    console.log('this.inputs.length = ', this.inputs.length);
    // the length is 0
}

DEMO

Ksthawma
  • 1,257
  • 1
  • 16
  • 28
  • You can't query TextField, try using template reference variable like `` and in component `@ViewChildren('ref') inputs: QueryList;` – yurzui Apr 14 '18 at 17:25
  • @yurzui - Why can't we query `TextField`'s? – ConnorsFan Apr 14 '18 at 17:31
  • [The Angular api](https://angular.io/api/core/ViewChildren) queries component class name `@ViewChildren(Pane) ...`. – Ksthawma Apr 14 '18 at 17:32
  • `@ViewChildren(Pane)` is working because `Pane` is class not selector. In link you have shared it's stated under `Metadata Properties` > "selector - the directive type or the name used for querying." – Anshuman Jaiswal Apr 14 '18 at 17:35
  • @ConnorsFan See answer below – yurzui Apr 14 '18 at 17:46
  • @yurzui - OK, I see. I thought that `TextField` was an Angular component class. If it was, the original code would work, I guess (I hope so because I use `ViewChildren` that way in my own code). Thanks for the explanation. – ConnorsFan Apr 14 '18 at 17:52

2 Answers2

2

Add template variable textField (it could be anything) in template as:

<StackLayout class="home-panel">
    <TextField #textField hint="Enter text1..."></TextField>
    <TextField #textField hint="Enter text2..."></TextField>
</StackLayout>

Then this will result as expected:

@ViewChildren('textField') inputs: QueryList<ElementRef>;
Anshuman Jaiswal
  • 5,352
  • 1
  • 29
  • 46
1

There is a great answer about what we can query

So we can query angular directives that match template element, but there is no directive with TextField selector. TextField is nativescript component not angular.

Query TextValueAccessor

There is only TextValueAccessor directive but in order to query it you should honor its selector:

TextField[ngModel],TextField[formControlName],TextField[formControl],

For example, the following should work:

template

<StackLayout class="home-panel">
    <TextField hint="Enter text1..." ngModel></TextField>
    <TextField hint="Enter text2..." ngModel></TextField>
                                     ^^^^^^^
                                 notice attribute here
</StackLayout>

component.ts

import { TextValueAccessor } from 'nativescript-angular/forms/value-accessors';

...

@ViewChildren(TextValueAccessor) inputs: QueryList<TextField>;

Query ElementRef

Otherwise use template reference variable:

template

<TextField #ref></TextField>

component.ts

@ViewChildren('ref') inputs: QueryList<ElemenetRef>;
yurzui
  • 205,937
  • 32
  • 433
  • 399
  • 1
    This is a detail explanation so I chose this as the accepted answer. Anshuman Jaiswal's answer is quick and straight to the point though. If you just want to have things work, see Anshuman Jaiswal's answer. – Ksthawma Apr 15 '18 at 07:36