从对象中,我只需要拾取所需的数字即可。例如,我有一个3位的Number
,我想在第二个跨度的第一个span
中显示第一位数字,如下所示。
如何获得?有没有一种过滤方法可以做到这一点?
这是我的代码:
var myApp = angular.module('myApp', []);
myApp.controller('count', function($scope) {
$scope.digital = 849;
$scope.total = 105631466;
});
HTML:
<div class="container" ng-app="myApp">
<div class="content" ng-controller="count">
<span class"firstDigit">{{digital}}</span><!-- only first digit(8) -->
<span class"secondDigit">{{digital}}</span> <!-- only second digit(4) -->
<span class"thridDigit">{{digital}}</span> <!-- only third digit(9) -->
<span>{{total}}</span> <!-- need to show like 75,688,6497 (adding commas) -->
</div>
</div>
jsfiddle
最佳答案
您可以使用$scope.digital
将$scope.digitalArray = $scope.digital.toString().split('');
转换为数组
然后使用ng-repeat
遍历新数组。
这样就可以拥有任意长度的整数,并且它们都将出现
使用{{total | number}}
会将其格式化为逗号,就像您正在寻找
JSFiddle
的JavaScript
var myApp = angular.module('myApp', []);
myApp.controller('count', function($scope) {
$scope.digital = 849;
//Could also use $scope.digitalArray = $scope.digital.toString();
//But I think it's better for learning purposes to split it into an array
$scope.digitalArray = $scope.digital.toString().split('');
$scope.total = 105631466;
});
的HTML
<div class="container" ng-app="myApp">
<div class="content" ng-controller="count">
<div ng-repeat="n in digitalArray">
<span>{{n}}</span><!-- only first digit(8) -->
</div>
<span>{{total | number}}</span> <!-- need to show like 75,688,6497 (adding commas) -->
</div>
</div>