微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs ngRepeat orderBy当属性键有空格时

我得到一些 JSON格式的数据,其中一些键中有空格:
[
    {
        "PlainKey": "SomeValue","Spaced Key": "SomeValue"
    },{
        "PlainKey": "SomeValue2","Spaced Key": "SomeValue2"
    }
]

这发生在某个时刻:

$http.get('https://dl.dropBoxusercontent.com/u/80497/htmlTesting/properties/credits.properties' + '?callback=JSON_CALLBACK').then(function (data) {
            $scope.credits = data.data;
        },function (error) {
            $scope.errorOccured = true;
            console.log("Error:");
            console.log(error);
        });

然后使用ng-repeat来显示它,并订购:

<select ng-model="corder">
  <option value="PlainKey">Plain Key</option>
  <option value="Spaced Key">Spaced Key</option>
</select>

<li ng-repeat="credit in credits | orderBy:corder" >
.....
</li>

这不起作用(我得到一个例外)
(PlainKey可以工作,因为没有空格).

我也尝试将值放在’:

<select ng-model="corder">
  <option value="'PlainKey'">Plain Key</option>
  <option value="'Spaced Key'">Spaced Key</option>
</select>

这似乎改变了顺序,但不正确.

我错过了什么?

谢谢!

评论似乎有帮助所以我将其作为答案包括在内:

在对象属性名称中对带有空格的项进行排序的一种方法是将谓词排序函数传递给orderBy,而不是指定对象属性名.特别相关的修改

HTML:

<li ng-repeat="credit in credits | orderBy:predicate">

JS:

$scope.predicate = function(val) {
  // $scope.corder corresponds to the object property name to sort by
  return val[$scope.corder];
}

示范Plunker.

原文地址:https://www.jb51.cc/angularjs/141448.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐