Adding function to Json Array in AngularJS

I have this JSON object returned from webapi,


Open in new window

I store this array in "$scope.dtArray" which is bound to the UI(textboxes) using ng-repeat. before the binding happens I want to assign a function to add values as shown here

this.val3 = function() {return Number(this.val1) + Number(this.val2) };

for each object in the array. SO when ever the user changes the val1 and val2 text boxes, the value changes for val3 automatically.

Following is the UI markup...

<div ng-repeat="cnt in dtArray track by $index">
    <table style="border-style: solid;">
                <input type="text" ng-model="">
                <input type="text" ng-model="cnt.val1">
                <input type="text" ng-model="cnt.val2">
                <input type="text" ng-model="cnt.val3()">

Open in new window

the function is not getting called in change of value in val1 or val2.

Any help would be appreciated.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


<input type="text" value="{{val3()}}" >

though it totally does not make sense to use an input for a calculated value. You cannot use ng-model on calculated fields since its not writable.
test page :

var myApp = angular.module("myApp", []);
var myCtrl = myApp.controller("myCtrl", ["$scope", function($scope) {
    $scope.dtArray = [
        { name:"John", val1:10, val2:20, val3:0 },
        { name:"Jane", val1:50, val2:200, val3:0 }

Open in new window

Srinivasulu MuppalaLead DeveloperCommented:
Hi replace the line <input type="text" value="{{val3()}}" /> to <input type="text" value="{{cnt.val1 + cnt.val2}}" />

The model in "val3" is actually dtArray[$index].val3, so the function, which is going to change it, must be provided with an index. Angular's ng-model is two way binding, so one just needs to change dtArray and the rest gets updated automatically. ng-repeat adds $index for each element.

We want this to happen when val1 or val2 changes, so we need to trap the ngChange event and call a function which does the calculation :-

$scope.updateVal3(index) {
   $scope.dtArray[index].val3 = $scope.dtArray[index].val1 + $scope.dtArray[index].val2;

and then add to the input elements :-

<input type="text" ng-model="cnt.val1" ng-change="updateVal3($index)">
<input type="text" ng-model="cnt.val2" ng-change="updateVal3($index)">

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.