how to get value from textbox using typescript in angular 2
if you get without using ngModel:
var num1= ((document.getElementById("num1") as HTMLInputElement).value);
var num2= ((document.getElementById("num2") as HTMLInputElement).value);
var result=parseInt(num1)+parseInt(num2);
console.log(result);
HTML
<p>first number:<input type="number" [(ngModel)]='a'></p>
<p>second number:<input type ="number" [(ngModel)]='b'></p>
<h1>{{a + b}}</h1>
Component
export class AppComponent
{
a: number = 0; //set default value as 0
b: number = 0;
}
The easiest way is to use template reference variable:
@Component({
selector: 'app-little-tour',
template: `
<input #newHero >
<button (click)="addHero(newHero.value)">Add</button>
})
export class LittleTourComponent {
addHero(newHero: string) {
console.log(newHero)
}
}
A complete guide for User Input in Angular can be found here: https://angular.io/guide/user-input
HTML
<p>first number:<input type="text" id="num1" [(ngModel)] = "value" ></p>
<p>second number:<input type ="text1" id="num2" [(ngModel)] = "value1"></p>
<h1> {{value + value1}}</h1>
Component class
export class AppComponent {}