0

I want to add two values using Angular js . I have fetched values from database using $http.get($scope.url) and using the values on html page. now I want to add " 1" in the value. my code is :- app.js

mainApp.controller("displayController", ['$scope', '$http', 'shareDataService', function($scope, $http, shareDataService) {     
        $scope.url = 'incomeBenefit.php';
        $http.get($scope.url)
        .then(function (response) {$scope.names = response.data.records;                        
            for(X in $scope.names){ 
                var t_age = $scope.names[X]['client_age'];                                                                                      
                var t_premium = $scope.names[X]['premium'];                                                                                                     
            }           
            $scope.t_age = t_age;
            $scope.t_premium = t_premium;   
        });
}]);

and my html page :-

        <ul id="form-bt-main" class="sec-row" ng-repeat="n in [] | range:31">
            <li class="form-bt">{{n}}</li>
            <li class="form-bt">{{t_age = t_age +1}}</li>   
        </ul>

I want to add '1' in t_age. t_age = '24' and want values like this 24 25 26 27 28 29 30 in li on output screen.

8
  • 2
    What about {{t_age + $index + 1}}? Commented Aug 1, 2016 at 11:35
  • its not working. not adding the values . its giving me 2401 . Commented Aug 1, 2016 at 11:37
  • 1
    parseInt(t_age, 10) + 1 Commented Aug 1, 2016 at 11:37
  • 1
    {{t_age ++ $index ++ 1}} it will work please test it. One + for concatenation and other for adding two numbers Commented Aug 1, 2016 at 11:38
  • 1
    @Ranjeetsingh ji i have edited my answer and it's tested try it and if work thums up!! Commented Aug 1, 2016 at 12:05

6 Answers 6

2
angular.module('app',[]).controller('ctrl',function($scope){
        $scope.age = parseInt('24');
        $scope.names = ["singh","jorawar","kiran"];                
   });
<body ng-controller="ctrl">

<div ng-repeat="name in names">
    {{name}}<br>
    {{age + $index - 1 + 1}}
</div>
</body>
Sign up to request clarification or add additional context in comments.

Comments

0

<li class="form-bt">{{t_age = t_age +1}}</li> is not valid.

just write <li class="form-bt">{{t_age +1}}</li>

or if you want that value in controller as ng-model, try this

<li class="form-bt" ng-model="ages[$index]">{{t_age +1}}</li>

Comments

0

$scope.t_age = t_age; might be a string, just use parseInt(t_age); A simple fiddle to help..

<div ng-app="">
  <div ng-controller="MyCtrl">
      {{age + 1}}
  </div>
</div>

JS:

function MyCtrl($scope) {
  $scope.age = parseInt('24');
}

http://fiddle.jshell.net/0wh13nrz/1/

Comments

0

In your controller, you have to parse your age which is a string into a int

parseInt('string', 10);  // parseInt with radix

CONTROLLER

mainApp.controller("displayController", ['$scope', '$http', 'shareDataService', function($scope, $http, shareDataService) {     
        $scope.url = 'incomeBenefit.php';
        $http.get($scope.url)
        .then(function (response) {$scope.names = response.data.records;                        
            for(X in $scope.names){ 
                var t_age = parseInt($scope.names[X]['client_age'], 10);                                                                                      
                var t_premium = $scope.names[X]['premium'];                                                                                                     
            }           
            $scope.t_age = t_age;
            $scope.t_premium = t_premium;   
        });
}]);

Now you variable t_age is an integer

Comments

0

What kind of array is names. If it has name object then seeing your controller your html code must look something like this-

<ul id="form-bt-main" class="sec-row" ng-repeat="n in names | range:31">
    <li class="form-bt">{{n.name}}</li>
    <li class="form-bt">{{n.client_age = n.client_age + 1}}</li>
    <li class="form-bt">{{n.premium}}</li>
    <li class="form-bt">0</li>
    <li class="form-bt">27000</li>
    <li class="form-bt">0</li>
</ul>

and your controllers should not have loop to manipulate the names array, ng-repeat does that looping. Your controller should also change to -

mainApp.controller("displayController", ['$scope', '$http', 'shareDataService', function($scope, $http, shareDataService) {     
        $scope.url = 'incomeBenefit.php';
        $http.get($scope.url)
        .then(function (response) {
            $scope.names = response.data.records;
        });
    }]);

Comments

0

If you need to output values like that, try to ng-repeat number. Example:

<ul>
    <li ng-repeat="i in getNumber(7)">{{$index+number}}</li>
</ul>

And in your controller:

$scope.number = parseInt('24');
$scope.getNumber = function(num) {
    return new Array(num);   
}

live demo (jsfiddle).

full post about that trick is here

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.