Does anybody know how I can SKIP JSON ordering altogether when I use ng-repeat (in a painless way probably)?
For example, my source JSON looks something like this -
{
"title": "Title",
"description": "Description",
"moreInfo": "Moreinformation"
}
Once I use it in ng-repeat, it orders them alphabetically. Something like this -
{
"description": "Description",
"moreInfo": "Moreinformation",
"title": "Title"
}
My ng-repeat looks something like this -
<div ng-repeat="(key,data) in items">
<span class="one"> {{key}} </span>
<span class="two"> {{data}} </span>
</div>
I've seen people having a separate array of the keys and using them to identify the JSON objects, ultimately avoiding alphabetical sorting.
Is there an elegant way to do this?
You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.
You can use unique filter while using ng-repeat . If you use track by $index then unique won't work.
If you have an collection of objects, the ng-repeat directive is perfect for making a HTML table, displaying one table row for each object, and one table data for each object property.
Stay on the bleeding edge of your favorite technologies. $index is a way to show which iteration of a loop you're in. If we set up an ng-repeat to repeat over the letters in 'somewords', like so: <div ng-app="app"> <div ng-repeat="item in 'somewords'.split('')"> {{$index + 1}}. {{item}} </div> </div>
Nice workaround found at google groups:
<div ng-repeat="key in notSorted(data)" ng-init="value = data[key]">
<pre>
key: {{key}}
value: {{value}}
</pre>
</div>
And in scope:
$scope.data = {
'key4': 'data4',
'key1': 'data1',
'key3': 'data3',
'key2': 'data2',
'key5': 'data5'
};
$scope.notSorted = function(obj){
if (!obj) {
return [];
}
return Object.keys(obj);
}
Working: http://jsfiddle.net/DnEXC/
Original: https://groups.google.com/forum/#!topic/angular/N87uqMfwcTs
This answer using filter works best for me.
https://groups.google.com/d/msg/angular/N87uqMfwcTs/EGoY6O2gtzsJ
http://jsfiddle.net/er52h/1/
angular.module('myFilters', [])
.filter('keys', function() {
return function(input) {
if (!input) {
return [];
}
return Object.keys(input);
}
});
You can use like this:
<div ng-repeat="key in data | keys" ng-init="value = data[key]"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With