BenSV
BenSV

Reputation: 169

Appending a value to a string variable in TypeScript

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.

Upvotes: 1

Views: 1807

Answers (1)

Prakash Harvani
Prakash Harvani

Reputation: 1041

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);
  }

Upvotes: 3

Related Questions