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> 这不起作用(我得到一个例外) 我也尝试将值放在’: <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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |