3

html

<input #box (keyup.enter)="Character(box.value)"/>

typescript

Character(value: string) {
  console.log("print this " + value);
}

I confirmed the typescript code in two different places on the net and got the html code from another answer on here and even though the typescript function executes the "value" just will not print to console. I even changed my browser theme thinking that was interfering at one point. (because it changes the color of textboxes and one some sites the text blends with the box)

Update As you can tell from the selected answer I didn't realize my issue was that I had two textboxes which was causing the issue because the value wasn't just getting past to both as I assumed.

3
  • Honesty why the downvote? Commented Oct 31, 2018 at 8:09
  • It's working, you can check here stackblitz.com/edit/angular-2hkxz1 Commented Oct 31, 2018 at 8:13
  • @trichetriche I figured that might have been why I was downvoted but I don't know what else so show. The project made from ng new and most of the code is still that stuff. Commented Oct 31, 2018 at 9:12

2 Answers 2

1

This working as expected :

Refer: https://stackblitz.com/edit/angular-jcrcdu?file=src%2Fapp%2Fapp.component.html

app.component.html

<input #box (keyup.enter)="Character(box.value)"/>

<p>{{box1name}}</p>




<input #box2 (keyup.enter)="onEnter(box2.value)"/>

<p>{{box2name}}</p>

app.component.ts

 import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  box1name = 'Angular';
  box2name = 'Angular2';

  Character(value: string) {
  this.box1name="print this for box1 :" + value;
  console.log(this.box1name);
}

  onEnter(value: string) {
  this.box2name="print this for box2: " + value;
  console.log(this.onEnter);
}


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

7 Comments

That's so weird because in my firefox console it doesn't show. See my screenshot. ibb.co/jyhznf
this is working in firefox also press enter key after type
I did that's how th other text which is a direct string gets triggered to print. (the text that does show in the screenshot) I'll try the dev firefox.
which version of firefox you using
the demo you posted works for me too but my own thing doesn't. ff version is ff quantum 63.0 dev version didn't make a difference.
|
0

what you can do is wrap you input in form tag, like:

<form (ngSubmit)="submit()">
    <input type="text" [(ngModel)]="name" name="name">
</form>

In typescript:

name = '';
submit() {
  console.log(this.name);
}

2 Comments

The typescript function runs as expected but the text of the var doesn't show. ibb.co/jyhznf
@codehelp4 it will call submit method when you press enter

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.