将字符串表转换为带范围的int

时间:2015-10-18 17:21:37

标签: javascript angularjs

我有一个名为World.json的大文件,其中包含全球所有国家/地区的大量数据。我一直在使用列表来显示表中的所有数据。我想按顺序排序所有值。但问题是我在world.json中的所有 int值都是字符串。我知道我需要使用parseInt使其工作,但尝试了一切没有任何成功

只是代码的例子 的 World.js

"Country": [
{
  "Code": "AFG",
  "Name": "Afghanistan",
  "Continent": "Asia",
  "Region": "Southern and Central Asia",
  "SurfaceArea": "652090.00",
  "IndepYear": "1919",
  "Population": "22720000",
  "LifeExpectancy": "45.9",
  "GNP": "5976.00",
  "GNPOld": "NULL",
  "LocalName": "Afganistan/Afqanestan",
  "GovernmentForm": "Islamic Emirate",
  "HeadOfState": "Mohammad Omar",
  "Capital": "1",
  "Code2": "AF"
},
{

controller.js

countryApp.controller('ctrlMain', function($scope, $http) {
  $http.get('world.json').success(function(data){
    $scope.world = data;
  });

  });

 countryApp.controller('detailsCtrl', function($scope, $routeParams){
$scope.name = $routeParams.countryName;
});

all.html

<td class="heading"><a href="" ng-click="reverse = predicate == 'LifeExpectancy' && !reverse; predicate = 'LifeExpectancy'">Befolkningsm&aumlngd</a></td>

<tr ng-repeat="country in world.Country | filter: { Continent: vDel } | filter:search | orderBy: predicate:reverse">
  <td>{{ country.Population }}</td>
</tr>

1 个答案:

答案 0 :(得分:0)

有几种方法可以满足您的需求。最好的方法是在服务器上更新JSON。如果这不是一个选项,那么对ng-repeat的更新将起到作用。

<tr ng-repeat="country in world.Country | filter:search | orderBy:'Population*1':reverse">
  <td>{{ country.Population | number}}</td>
</tr>

我已经设置了一个在orderBy

中显示此功能的plunker

http://plnkr.co/edit/ocD0TskHAqULHUddDPku?p=preview

编辑:更新了plunker,因此反向工作。