Reputation: 1263
through ng-repeat
I want to plot videos in a defined order according to the id
number (from 1 to 5).
<div ng-app="myApp" ng-controller="Ctrl">
<div ng-repeat="vidObj in videoObjects | orderBy: 'id'">
<span>{{vidObj.id}}</span>
</div>
</div>
I try to apply the filter orderBy
, but apparently is not working properly. Do you have an idea of what I am doing wrong? This is a JSfiddle with the complete code.
Thanks in advance for your replies!
EDIT: Thank you for your replies, but the json
file can not change.
Upvotes: 0
Views: 140
Reputation: 2679
The Javascript :
angular.module('myApp', [])
.controller('Ctrl', function($scope) {
$scope.videoObjects = {
"Frank": {
"id": 1
},
"Gerard": {
"id": 2
},
"Rein": {
"id": 3
},
"Ida": {
"id": 4
},
"Ellen": {
"id": 5
}
};
$scope.arr = Object.keys($scope.videoObjects).map(function(k) { return $scope.videoObjects[k] });
console.log($scope.arr);
});
and the HTML
<div ng-app="myApp" ng-controller="Ctrl">
<div ng-repeat="vidObj in arr | orderBy: 'id'">
<span>{{vidObj.id}}</span>
</div>
</div>
I updated your fiddle with this code.. It is working well.
Here is the link http://jsfiddle.net/tirthrajbarot/fc16op6u/39/
Upvotes: 1
Reputation: 3984
Try a valid json, somthing like:
angular.module('myApp', [])
.controller('Ctrl', function($scope) {
$scope.videoObjects = [
{
"id": 1,
"Frank":'name'
},
{
"id": 2,
"Gerard":'name'
}
];
});
Upvotes: -2
Reputation: 1894
I think that the built-in orderBy filter works with arrays.
The documentation says "The collection can be an Array or array-like object (e.g. NodeList, jQuery object, TypedArray, String, etc)."
Check this example in the documentation that says "Example Ordering a table with ngRepeat".
Upvotes: 1
Reputation: 192
I modified your Json and it works
angular.module('myApp', [])
.controller('Ctrl', function($scope) {
$scope.videoObjects = [
{
"name": "Frank",
"id": "1"
},
{
"name": "Gerard",
"id": "2"
},
{
"name": "Rein",
"id": "3"
},
{
"name": "Ida",
"id": "4"
},
{
"name": "Ellen",
"id": "5"
}
]
});
Upvotes: 1