加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs ngRepeat orderBy当属性键有空格时

发布时间:2020-12-17 08:21:04 所属栏目:安全 来源:网络整理
导读:我得到一些 JSON格式的数据,其中一些键中有空格: [ { "PlainKey": "SomeValue","Spaced Key": "SomeValue" },{ "PlainKey": "SomeValue2","Spaced Key": "SomeValue2" }] 这发生在某个时刻: $http.get('https://dl.dropboxusercontent.com/u/80497/htmlTes
我得到一些 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读