I have an ng-repeat that looks like this:
<div ng-app="questionnaire">
<div ng-controller="QuestionnaireCtrl">
<div ng-repeat="(key,val) in questions | orderBy:key">
{{questions[key].question}}
{{questions[key].answer}}
</div>
</div>
</div>
The controller looks like this:
(function(){
var app = angular.module('questionnaire',[]);
app.controller('QuestionnaireCtrl', function($scope){
$scope.questions = {
someItem: { question : 'First question' },
anotherItem: { question : 'Next question here' },
upgradeItem: { question : 'Another one' }
};
});
})();
Now the ng-repeat works fine but displays the questions in a random order. I've tried using orderBy
but can't get it working. I just want the questions and answers (which aren't currently in the array) to display in order of index (i.e. the order they're in the array). Alternatively I could add another field 'section' to the array and display them filtered in that order. E.g.
$scope.questions = {
someItem: { question : 'First question', section : '1' },
anotherItem: { question : 'Next question here', section : '1' },
upgradeItem: { question : 'Another one', section : '2' }
};
Fiddle example here: http://jsfiddle.net/k52ez/