If you're looking to achieve this task, there are various methods you can explore:
One way is to utilize a template variable for the input
element to extract its value
like so:
<input #yourInput type="text" (keyup.enter)="sendit(yourInput.value)" />
To retrieve the value in the Component, you can do the following:
sendit(inputValue) {
console.log(inputValue);
}
Alternatively, you can also opt for using [(ngModel)]
for two-way binding with the text field:
<input
type="text"
[(ngModel)]="bindedTwoWays"
(keyup.enter)="sendit(bindedTwoWays)" >
And in the Component:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
bindedTwoWays = 'Angular';
sendit(inputValue) {
console.log(inputValue);
}
}
If you want to see a demonstration, check out this Working Sample StackBlitz showcasing both of these approaches.