sanu
sanu

Reputation: 1068

Angularjs Adding two numbers

Hi I want to add two field and put in another field

<input type="text" ng-model="pfi.value1">
<input type="text" ng-model="pfi.value2">
<input type="text" ng-model="pfi.sum" >

its working fine in label

<label>{{ pfi.value1 + pfi.value2}}</label>

but how to do same thing in text field

Upvotes: 0

Views: 73

Answers (4)

georgeawg
georgeawg

Reputation: 48948

You can do it in the template

<input type="number" ng-model="pfi.value1">
<input type="number" ng-model="pfi.value2">
<input type="number" ng-model="pfi.sum" >

<p>{{ pfi.sum = pfi.value1 + pfi.value2}}</p>

The $interpolation service evaluates the exoression on each change to the inputs and updates the sum.

The DEMO on JSFiddle.

Upvotes: 1

z.a.
z.a.

Reputation: 2777

you should do that operation in your controller, assuming you are using pfi for controllerAs attribute?

x.controller('xctrl', function() {
    var pfi = this;
    pfi.sum = pfi.value1 + pfi.value2;
});

Upvotes: 0

Alemndra
Alemndra

Reputation: 26

you should do it on the controller pfi.sum = pfi.value1 + pfi.value2 also,you need to add the controller to your html file.

Upvotes: 0

senschen
senschen

Reputation: 804

You should set pfi.sum = pfi.value1 + pfi.value2; inside your controller. I'm not positive what the two-way binding would do if you then edited the text field attached to pfi.sum, but I suspect it wouldn't be good. However, for display purposes, this should work.

Upvotes: 2

Related Questions