【发布时间】:2014-09-26 17:25:46
【问题描述】:
我有一个看起来像这样的 ng-repeat:
<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>
控制器如下所示:
(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' }
};
});
})();
现在 ng-repeat 可以正常工作,但会以随机顺序显示问题。我试过使用orderBy,但无法正常工作。我只希望问题和答案(当前不在数组中)按索引顺序显示(即它们在数组中的顺序)。或者,我可以将另一个字段“部分”添加到数组中,并按该顺序显示它们。例如
$scope.questions = {
someItem: { question : 'First question', section : '1' },
anotherItem: { question : 'Next question here', section : '1' },
upgradeItem: { question : 'Another one', section : '2' }
};
这里的小提琴示例:http://jsfiddle.net/k52ez/
【问题讨论】:
-
这可能会有所帮助:stackoverflow.com/questions/18124665
标签: javascript html arrays angularjs