1

I'm new to Angular.I'm trying to get the user input of a text box in each keystroke and append it to a string variable like on java.

in the text box I use (keyup)="onKey($event)" as follows,

<input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Departure Airport"  id="departure" name="departure" (keyup)="onKey($event)" [(ngModel)]="booking.departure" >

Then I used that and tried to append each letter entered to a variable as follows

export class BookingComponent implements OnInit {
  

  public  userInPut="";

  constructor(private flightService:FlightService, private router: Router) { }

  ngOnInit(): void {

  }

  onKey(event: any){
    this.userInPut=this.userInPut+event;
   console.log(this.userInPut);
  }


}

I expected that the output would be a,ab,abc,abcd like that but the out put was a set of objects as shown in the figure below The console output

What I wanted to do was display that userInPut variable value under the textbox.Are all variables created in typescript are object form.Please give me a clarification and get the expected output.

3
  • The first argument of an event handler will always (unless you explicitly changed the behavior) be an event object, hence the [object KeyboardEvent]. Have a look at the documentation what properties a KeyboardEvent has and what might solve your problem. Commented Jul 18, 2020 at 12:07
  • any reason not to use [(ngModel)]? Do you really need to listen for each keystroke? Commented Jul 18, 2020 at 12:48
  • @Clashsoft I'm trying to develop a autocomplete textbox Commented Jul 18, 2020 at 13:03

1 Answer 1

3

If you need output like (a,ab,abc,abcd) then try this:

In HTML file:

     <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Departure Airport"  id="departure" name="departure" (keyup)="onkeyUp($event.target.value)">

In TS file:

userInPut: any = '';

  onkeyUp(value) {
    this.userInPut = value;
    console.log(this.userInPut);
  }
Sign up to request clarification or add additional context in comments.

Comments

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.