我想使用Angular js添加两个值。我使用$ http.get($ scope.url)并使用html页面上的值从数据库中获取值。现在我想添加" 1"在价值中。我的代码是: - 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;
});
}]);
和我的html页面: -
<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>
我想添加&#39; 1&#39;在t_age。 t_age =&#39; 24&#39;并且在输出屏幕上想要这样的值24 25 26 27 28 29 30。
答案 0 :(得分: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>
答案 1 :(得分:0)
<li class="form-bt">{{t_age = t_age +1}}</li>
无效。
只需撰写<li class="form-bt">{{t_age +1}}</li>
或者如果您希望控制器中的值为ng-model
,请尝试此
<li class="form-bt" ng-model="ages[$index]">{{t_age +1}}</li>
答案 2 :(得分:0)
$scope.t_age = t_age;
可能是string
,只需使用parseInt(t_age);
一个简单的小提示来帮助..
<div ng-app="">
<div ng-controller="MyCtrl">
{{age + 1}}
</div>
</div>
JS:
function MyCtrl($scope) {
$scope.age = parseInt('24');
}
答案 3 :(得分:0)
在您的控制器中,您必须将您的年龄即字符串解析为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;
});
}]);
现在变量t_age
是一个整数
答案 4 :(得分:0)
名称是什么类型的数组。如果它有名称对象,那么看到你的控制器你的html代码必须看起来像这样 -
<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>
并且你的控制器不应该有循环来操作names数组,ng-repeat执行循环。您的控制器也应更改为 -
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;
});
}]);
答案 5 :(得分:0)
如果您需要输出这样的值,请尝试ng-repeat number。例如:
# Once in either Terminal
mkfifo /tmp/A /tmp/B
# In first Terminal
( CommandA; echo done > /tmp/A ) &
# In second Terminal
( CommandB; echo done > /tmp/B ) &
# In third Terminal
read < /tmp/A; read < /tmp/B; commandRest
在你的控制器中:
<ul>
<li ng-repeat="i in getNumber(7)">{{$index+number}}</li>
</ul>
直播 demo (jsfiddle)。
关于该技巧的完整帖子是here