0

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

2
  • 2
    Does the same thing not work? Show what's not working Commented Mar 30, 2016 at 17:16
  • how to this in form field i tried <input type="text" ng-model="pfi.sum" ng-value="pfi.value1 + pfi.value2" > Commented Mar 30, 2016 at 17:18

4 Answers 4

2

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.

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

Comments

1

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.

Comments

0

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

1 Comment

yes i tried $scope.pfi.sum = $scope.pfi.value1 + $scope.pfi.value2 TypeError: Cannot read property 'value1 ' of undefined
0

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

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.