ngopal
ngopal

Reputation: 219

how to get value from textbox using typescript in angular 2

I am a beginner to angular2 / typescript I'm trying get numbers from two textboxes and adding both numbers and display the result using interpolation

@Component({
selector: 'my-app',
template: 
`<h1>Hello {{name}}</h1>
<h1>{{D}}</h1>
 <form>
  <p>first number:<input type="text" id="num1"></p>
  <p>second number:<input type ="text1" id="num2"></p>
  <h1> {{result}}</h1>
  </form>
   <test-app></test-app>`
  })

  export class AppComponent 
  { 
 name = 'Angular'; 
 value : number;value1 : number;result:number;
  constructor(value : number,value1 : number,result:number)
  {

  this.value = parseFloat
  ((document.getElementById("text") as HTMLInputElement).value);
  this.value1 = parseFloat((document.getElementById("text1") 
  as HTMLInputElement).value);
  this.result=this.value+this.value1;
  }}

Upvotes: 18

Views: 117940

Answers (7)

Saurabh Ranjan
Saurabh Ranjan

Reputation: 31

app.component.html

<div>
      <input type='text' [(ngModel)]='userName' placeholder='user name' class='user_id_text'>
      </div>
      <div>
      <input type='password' [(ngModel)]='password' placeholder='password' class='user_id_password'>
      </div>
      <div>
      <input type='submit' value='Login' (click)='registeredUser($event)'>
      </div>

app.component.ts

export class AppComponent implements OnInit{
userName ='';
password = '';

ngOnInit(){
  console.log(this.userName+' password -'+this.password);
}
registeredUser(){
 let {userName,password} = this;

 if(userName=='ssss' && password == 'ssss'){
    console.log(userName+' password -'+password);
 }
}

} Explanation - ngModel with [()] is two-way binding. the value in the UI always syncs back to the domain model in your class

Upvotes: 3

ttfreeman
ttfreeman

Reputation: 5523

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

Upvotes: 23

Nabin Kumar Khatiwada
Nabin Kumar Khatiwada

Reputation: 1646

html :

 <input type="text" id="obj_id" >

ts > read as :

 (<HTMLInputElement>document.getElementById(obj_id)).value

If object is involved such as in for loop,text boxes are to be given ids from object field values then Use [id] instead of id

<input type="text" [id]="obj.field"  >

Upvotes: 0

Prashant Yerekar
Prashant Yerekar

Reputation: 149

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

Upvotes: 14

Sreeragh A R
Sreeragh A R

Reputation: 3021

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;

}

Upvotes: 7

ranjeet8082
ranjeet8082

Reputation: 2359

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 {}

Upvotes: 9

Dmitrij Kuba
Dmitrij Kuba

Reputation: 1038

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>
  <form (ngSubmit)="onSubmit(f)">
      <p>first number:<input type="text" ngModel name="num1" id="num1"></p>
    <p>second number:<input type="text" ngModel name="num2" id="num2"></p>
</form>
<test-app></test-app>`
  })

export class AppComponent {
  name = 'Angular';

  constructor() {
  }

  onSubmit(f: NgForm) {
      console.log(f.form.value);
  }
}

Upvotes: 0

Related Questions