AngulsrJS $filter orderby 一个带有 @ 符号的数组键


{
    "@odata.count": 52,
    "value": [
        {
            "@search.score": 0.58868027,
            "LastDirSyncTime": "2015-02-27T08:22:22+00:00"
        },
        {
            "@search.score": 0.58868029,
            "LastDirSyncTime": "2015-03-27T08:22:22+00:00"
        },
        {
            "@search.score": 0.60868029,
            "LastDirSyncTime": "2014-03-27T08:22:22+00:00"
        },
        {
            "@search.score": 0.58861029,
            "LastDirSyncTime": "2014-03-27T08:22:22+00:00"
        }
    ]
}

我有上面我想orderBy我使用的 json 返回

var newdata = $filter('orderBy')(data, '@search.score');

但它返回Error: [$parse:lexerr] Lexer Error: Unexpected next character at columns 0-0 [@] in expression [@search.score].

有没有办法通过@search.score键让我的结果排序?

解析你的对象并从'@search.score'中删除'@search'

$scope.abc= [
{
  "score": 0.58,
  "LastDirSyncTime": "2015-02-27T08:22:22+00:00"
},
{
  "score": 0.59,
  "LastDirSyncTime": "2015-03-27T08:22:22+00:00"
},
{
  "score": 0.60,
  "LastDirSyncTime": "2014-03-27T08:22:22+00:00"
},
{
  "score": 0.52,
  "LastDirSyncTime": "2014-03-27T08:22:22+00:00"
}
];

然后你可以只使用'orderBy' (https://docs.angularjs.org/api/ng/filter/orderBy(

<table >
  <tr>
    <th>Score</th>
    <th>LastDirSyncTime</th>
  </tr>
  <tr ng-repeat="a in abc | orderBy:'score'">
    <td>{{a.score}}</td>
    <td>{{a.LastDirSyncTime}}</td>
  </tr>
</table>
var newdata = $filter('orderBy')(data, '"@search.score"');

orderBy需要一个表达式,而@search.score不被识别为有效的表达式。除了@之外,它还包含一个.,其解释与您预期的不同。

另一方面,"@search.score"是解释为属性名称的有效常量字符串表达式。

我避免在属性名称中使用点。

最新更新