angularjs 数据列表中数据更新以后的排序问题
发布于 2个月前 作者 yakczh 301 次浏览 来自 问答

排序字段是数值,初始化刷新的时候排序是对的,但是当修改其中某条纪录的排序字段的值以后,这个顺序就不对了 ,测试代码

<html >
        <head>
        <title></title>
<style type="text/css">


input {

font-size:64px;
}

</style>

        <script src="angular.js"></script>        </head>
<body ng-app="app.demo">
        <div ng-controller="ListCtrl">
<fieldset> <legend>users</legend>
<ul>
       <li ng-repeat="user in users | orderBy: 'weight'">  
                <a href="#" ng-click="show(user.id)" >{{user.name}} {{user.weight}}</a>  
         </li>
 </ul>
        </fieldset>
<fieldset> <legend>user</legend>
  
        <div ng-show="user" >
 <h1>    {{user.name}}  </h1>
                <input type="text" ng-model="user.weight" />
                
        </div>

 </fieldset>

</div>
<script type="text/javascript">
var myApp = angular.module("app.demo", []);

myApp.factory("Data", function(){
   return {users:[
{id:0,name:"第一名",weight:100},
{id:1,name:"第二名",weight:200},
{id:2,name:"第三名",weight:300},
{id:3,name:"第四名",weight:400},
{id:4,name:"第五名",weight:500}
]};

});

myApp.controller("ListCtrl", ["$scope", "Data", function($scope, Data) {
        $scope.users = Data.users;
 $scope.show = function(id){
       $scope.user= Data.users[id];
}  
}]);




</script>

</body>
</html>
4 回复

因为 AngularJS 的 {{ orderBy_expression | orderBy : expression : reverse}},这个 expression 把数值当做 string 对待,所以排序可能是乱的。

这个 expression 的文档见下,只接受 function,string,还有 Array

A predicate to be used by the comparator to determine the order of elements. Can be one of: function: Getter function. The result of this function will be sorted using the <, =, > operator. string: An Angular expression. The result of this expression is used to compare elements (for example name to sort by a property called name or name.substr(0, 3) to sort by 3 first characters of a property called name). The result of a constant expression is interpreted as a property name to be used in comparisons (for example “special name” to sort object by the value of their special name property). An expression can be optionally prefixed with + or - to control ascending or descending sort order (for example, +name or -name). If no property is provided, (e.g. ‘+’) then the array element itself is used to compare where sorting. Array: An array of function or string predicates. The first predicate in the array is used for sorting, but when two items are equivalent, the next predicate is used. If the predicate is missing or empty then it defaults to '+’.

得另外写 sort function 传入

@russj

myApp.factory("Data", function($http){ 
   var users={};
   $http.get('content.json').success(function(data) { 
        users=data;
  });
  
});

service 中换成外部数据, 其他Controller里就取不到 Data.users了 ,换成 ngresource也是一样 ,有什么办法可以让Data.users 在其他Controller里共享?

在每个 controller 的参数里注入啊

angular.
module('myServiceModule', []).
 controller('MyController', ['$scope','notify', function ($scope, notify) {
   $scope.callNotify = function(msg) {
     notify(msg);
   };
 }]).
factory('notify', ['$window', function(win) {
   var msgs = [];
   return function(msg) {
     msgs.push(msg);
     if (msgs.length == 3) {
       win.alert(msgs.join("\n"));
       msgs = [];
     }
   };
 }]);

其实最好的学习办法是找一本书来通读一下 比在网上问人来的快 除非是有很小众的问题 不然网上问人很慢的

回到顶部