<ion-card *ngFor='#product of products | mapToIterable:"key":true'>
<ion-list>
<ion-item>
<ion-label stacked>Account No. / Phone No.</ion-label>
<ion-input type="text" [(ngModel)]="product.msisdn"></ion-input>
</ion-item>
<ion-item>
<ion-label stacked>Amount</ion-label>
<ion-input type="text" (keypress)="isNumberKey($event)" [(ngModel)]="product.amount"></ion-input>
</ion-item>
</ion-list>
</ion-card>
Referring to the html above, how do I get the reference of ion-input so that I can setFocus()
on it after validation fails. I've already came out with the code below to validate each inputs.
for (var product of <any>this.products) {
if (product.service_type_id == 2 && !product.msisdn) {
alert('something');
//Get the element and set focus here.
}
}
Is this a good approach? Is there a better way of handling this in Angular 2?
An approach to get references of elements created with
*ngFor
is@ViewChildren()
If the elements are actually components or directives then the component/directive type can be passed as parameter otherwise a template variable can be added and the name of the template variable (as string) can be used to query the elements.
In this case both a template variable and the component type return the component instance but to call focus we need the
ElementRef.nativeElement
therefore I created a helper directive that is applied toion-input
and is used to query the elements and also to callfocus()
:See also Angular 2: Focus on newly added input element
Plunker example