2

I'm using an usb card reader with my application to fill an input form. I'm trying to hide it and be able to badge with my card to send the form.

my app

I tried to use

<input hidden type="text" id="cardNumber" formControlName="cardNumber" [(ngModel)]="refresh">

and

<input type="hidden" id="cardNumber" formControlName="cardNumber" [(ngModel)]="refresh">

and

<input type="text" style="display:none" id="cardNumber" formControlName="cardNumber" [(ngModel)]="refresh">

But as soon as the element disappear I'm unable to fill it with the card. I make sure the focus is always present by using

document.getElementById("cardNumber").focus();

I don't understand how angular treats in the DOM all hidden elements? But it looks like I can reach it but not use it.

2
  • 1
    Please also show us the code where you're trying to populate the form using your card reader Commented Jun 9, 2020 at 9:36
  • ngOnInit() { document.getElementById("cardNumber").focus(); } that's all the usb card reader will automatically write the content of the rfid card and press enter Commented Jun 9, 2020 at 10:12

3 Answers 3

4

You can even use

class="cdk-visually-hidden"

Documentation

Sign up to request clarification or add additional context in comments.

Comments

1

First of all, you should decide if you are going to hide your input and get it with pure js/html or using Angular capabilities, because the lines of code that you've posted are using vanilla javascript. Going that way you should try adding to the input css style with property visibility: hidden.

To make it hidden using Angular you should bind hidden property using square brackets notation like this: [hidden]="true".

And you should get the element in your code by initializing Angular Form and finding input by formControlName, here are the docs with explicit explanations how to achieve this: https://angular.io/api/forms/Form

If you have only one input and don't want to initialize the whole form for it, you may try to add ElementRef, the code would be something like this:

import {ElementRef} from '@angular/core';
@ViewChild('myInput') inputElement:ElementRef;

ngAfterViewInit() {
      this.inputElement.nativeElement.focus();
}

<input #myInput [hidden]="true" [(ngModel)]="refresh">

Comments

-1

What about hiding the field conditionally?

<input [hidden]="!isInputShown" type="text" id="cardNumber" formControlName="cardNumber" [(ngModel)]="refresh">

And in your ts something like this:

hideInput() {
    this.isInputShown = true;
}

hideInput() {
    this.isInputShown = false;
}

And then just call those methods to hide/show.

2 Comments

It won't work because I don't want see this field at all but I want he's present in the DOM. If we ask angular to remove it conditionnaly it won't be accesible.
Ok, then you need to use the [hidden] property. This will keep the element in DOM. Please check my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.