0

我有这个代码:

   var app = angular.module('app', []);
   app.controller('RadioCtrl', ['$scope', '$http',
     function($scope, $http) {
       $http.jsonp('http://www.filltext.com/?callback=JSON_CALLBACK&rows=5&fname={firstName}&lname={lastName}&id={index}').success(function(data) {
         $scope.people = data;
       })
     }
   ]);
<!DOCTYPE html>
<html ng-app="app">

<head>
  <meta charset="utf-8">
  <title>Radio button</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
</head>

<body>
  <section ng-controller="RadioCtrl">
    <input type="text" ng-model="pepek.fname">
    <br>Ascended
    <input type="radio" ng-model="direction" checked>Descended
    <input type="radio" ng-model="direction" value="reverse">
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>First Name</th>
          <th>Second name</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="person in people| filter:pepek |orderBy:person.id">
          <td>{{person.id}}</td>
          <td>{{person.fname}}</td>
          <td>{{person.lname}}</td>
        </tr>
      </tbody>
    </table>

  </section>
</body>

</html>

我想单击单选按钮升序或降序,我的表格将按 ID 排列。即使我尝试简单的orderBy:idor orderBy:-idor orderBy:person.idor or orderBy:id:reverseor orderBy:id:directionnot 这些案例没有按 ID 排列升序,它总是降序,我不知道为什么。先感谢您 :)

4

2 回答 2

1

ng-repeat 表达式不太正确。应该是这样的:

ng-repeat="person in people| filter:pepek | orderBy:'id':reverse"

并且单选按钮声明需要如下所示:

<input type="radio" ng-model="reverse" ng-value="false">Descended
<input type="radio" ng-model="reverse" ng-value="true">

var app = angular.module('app', []);
   app.controller('RadioCtrl', ['$scope', '$http',
     function($scope, $http) {
       
       $scope.reverse = false;
       
       $http.jsonp('http://www.filltext.com/?callback=JSON_CALLBACK&rows=5&fname={firstName}&lname={lastName}&id={index}').success(function(data) {
         $scope.people = data;
       })
     }
   ]);
<!DOCTYPE html>
<html ng-app="app">

<head>
  <meta charset="utf-8">
  <title>Radio button</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
</head>

<body>
  <section ng-controller="RadioCtrl">
    <input type="text" ng-model="pepek.fname">
    <br>Ascended
    <input type="radio" ng-model="reverse" ng-value="false">Descended
    <input type="radio" ng-model="reverse" ng-value="true">
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>First Name</th>
          <th>Second name</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="person in people| filter:pepek |orderBy:'id':reverse">
          <td>{{person.id}}</td>
          <td>{{person.fname}}</td>
          <td>{{person.lname}}</td>
        </tr>
      </tbody>
    </table>

  </section>
</body>

</html>

于 2014-11-14T11:45:12.423 回答
0

发生这种情况是因为您的 ID 可能被存储为字符串值而不是整数。尝试这个 :

<tr ng-repeat="person in people | orderBy:parseFloat(person.id)">
于 2014-11-14T10:53:01.370 回答