18

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;
  }}
5
  • 6
    angular.io/docs/ts/latest/guide/user-input.html Commented Apr 14, 2017 at 10:49
  • 2
    Binding to form elements is something that is covered in the very beginning of almost any angular tutorial including the many how-to's and tutorial on the main angular 2 site. Follow the link @echonax posted and see if you can figure it out. Commented Apr 14, 2017 at 10:51
  • have you ever read something in angular? Commented Apr 14, 2017 at 11:22
  • I have readed @Aravind Commented Apr 14, 2017 at 12:23
  • after reading you didnt get these information? Commented Apr 14, 2017 at 13:32

7 Answers 7

23

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

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

1 Comment

I got a problem when the input id is dynmic like #newHero{{i}}, I pass its value to the click method like this (click)="addHero(newHero{{i}}.vaue" but it cause compile error, I have no idea how to do it right , any help with a push in the right direction will be very much appreciated.
14

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

Comments

9

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

3 Comments

Thank you , for the answer but with using a button event how can we do that
Add a button in your html <button (click) = "addValues()">Add</button> . In class make addValues function. addValues() { this.result = this.value + this.value1; } replace value + value1 by result in h1 tag.
Also declare value, value1 and result variables in component class.
7

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;

}

1 Comment

problem will be that if a = 4 and b = 5 you want the total to sum , thus in h1 it should show 9 , but actually that is going to show 45
3
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

1 Comment

Very helpful that you show the complete solution. Only in my case it didn't like the ` symbol -- had to replace with ". Thanks!
0
@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);
  }
}

Comments

0

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"  >

Comments

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.