Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: How to use input [(ngModel)] value in Component?

Sorry if this is a basic question but I can't seem to figure it out.

Basically, I want to take an [(ngModel)] input value from the template and then pass it along to my component. I feel like this is pretty basic Angular 2 stuff but I can't seem to get it.

My code:

input.html:

<app-root>Loading...</app-root>

app/app.component.html

<input type="number" [(ngModel)]="value1">
<input type="number" [(ngModel)]="value2">
<!--I want my function value to update every time [(ngModel)] is updated
{{someFunction}}

app/app.component.ts:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  someFunction(): number {
     // I want to pass value1 and value 2 here from my ngModel in my template
     return value1 * value2
  } 
}
like image 894
user3183717 Avatar asked Mar 09 '23 14:03

user3183717


1 Answers

You have to declare these variables in your component.

export class AppComponent {
  value1: number;
  value2: number;
  someFunction() {
     return this.value1 * this.value2;
 } 
}

Working plunker

like image 107
kind user Avatar answered Mar 19 '23 15:03

kind user