当数组是服务内部对象的属性时,如何在页面上逐个显示数组元素
how to show array element one by one on page while array is property of an object which inside of services
我在Services:中有一个testPaper对象
this.testPapers=[{
courseName: 'Languages' , moduleName: 'Arabic',
paperName: 'Arabic-V',
paperDate: '15/08/2014',
// paperStartTime:
//papertEndTime:
// PaperDuration: '00:10:00',
marksForEachQuestion: 5,
totalMarks: 15,
Question: ['Mirwaha meaning in English?','Jwwaz meaning in English?','Hafila meaning in English?']
}
而控制器:
.controller('questionStart',function($scope, $routeParams,$rootScope,crudService){
// $scope.allCourses= crudService.courses;
// $scope.allModules=crudService.modules;
$scope.allTestPapers=crudService.testPapers;
}
和UI
<ol>
<li data-ng-repeat="p in allTestPapers | filter : mSelectedCourse
| filter: mSelectedModule
| filter : mSelectedPaper">
<hr>
<div class="row">
<div class="col-xs-12 col-sm-3">
Paper Name :<b>{{p.paperName}}</b>
</div>
<div class="col-xs-12 col-sm-3">
Total Marks:<b>{{p.totalMarks}}</b>
</div>
<div class="col-xs-12 col-sm-3">
Marks For Each Question: <b>{{p.marksForEachQuestion}}</b>
</div>
<div class="col-xs-12 col-sm-3">
Total Question: <b>{{p.Question.length}}</b>
</div>
</div>
<hr>
<h4>{{p.Question}}</h4>
<!--Which logic need to put in above line to not show all question at once instead one at a time
当用户点击一个按钮时,下一个问题将显示???如何做到这一点-->
<button data-ng-click="">next</button>
</ol>
现在,我想在页面上逐个显示问题数组(意思是从0到最后一项,但在页面上显示一个问题,然后点击按钮我想更改问题)。我怎样才能做到这一点;
因此,以下是您应该如何处理它,从UI开始,UI将有一个问题标题、一个答案输入框和一个下一个按钮:
<h3 id='q'>Questions will appear here<h3>
Answer: <input type:'text' id='a'><br>
<button onclick="showQuestion()">Next</button>
然后是你的tesPapers,在这个例子中,我使用了两篇论文:
var testPapers = [{
courseName: 'Languages' , moduleName: 'Arabic',
paperName: 'Arabic-V',
paperDate: '15/08/2014',
// paperStartTime:
//papertEndTime:
// PaperDuration: '00:10:00',
marksForEachQuestion: 5,
totalMarks: 15,
Question: ['Mirwaha meaning in English?','Jwwaz meaning in English?','Hafila meaning in English?']
},{
courseName: 'Languages' , moduleName: 'Arabic',
paperName: 'Arabic-V',
paperDate: '15/08/2014',
// paperStartTime:
//papertEndTime:
// PaperDuration: '00:10:00',
marksForEachQuestion: 5,
totalMarks: 15,
Question: ['Mirwaha meaning in English?','Jwwaz meaning in English?','Hafila meaning in English?']
}];
现在所有的问题逻辑,很简单。。你需要两个计数器,一个用于纸张编号,另一个用于问题编号。。当当前论文的问题结束时,您需要增加论文编号,当所有论文结束时,只需显示所有论文的结束。
var paperNumber = 0;
var questionNumber = 0;
function showQuestion(){
document.getElementById('q').innerHTML = testPapers[paperNumber].Question[questionNumber];
questionNumber++;
if(questionNumber > testPapers[paperNumber].Question.length){
document.getElementById('q').innerHTML = "End of this Paper";
questionNumber = 0;
paperNumber++;
if(paperNumber >= testPapers.length){
document.getElementById('q').innerHTML = "End of All Papers";
}
}
}
当然,你可以随心所欲地更改UI,但核心逻辑应该保持不变,特别是问答循环机制。
请参阅此处的演示
您需要将服务注入到所需视图的控制器中。然后你应该能够使用ng重复指令:
<p ng-repeat="quest in serviceName.testPaper[0].Question">{{quest}}</p>
根据注释进行编辑:在控制器中:
var index = 0;
$scope.currentQuestion = serviceName.testPaper[0].Question[index];
$scope.nextQuestion = function() {
$scope.currentQuestion = serviceName.testPaper[0].Question[index+1];
};
在html 中
<p>{{currentQuestion}}</p>
<button ng-click="nextQuestion()">Next Question</button>
相关文章:
- 在函数中添加数组元素的数值
- 访问JSON对象内部的数组元素
- Mongoose-在更新中删除数组元素
- 在输入数组元素时在 html 页面中显示数组元素
- 显示按表单构建的数组的数组元素总数
- *ngFor 在空数组中显示元素
- Jquery数组.如何同时显示与if语句匹配的数组元素
- 在jQueryUI对话框中显示和格式化数组元素
- 数组元素未在Javascript中显示
- 在php中,找到显示的数组元素,并提供到下一个和上一个的链接
- 当数组是服务内部对象的属性时,如何在页面上逐个显示数组元素
- 使用JavaScript显示数组元素
- ui-选择显示数组元素作为下拉菜单中的选项
- 删除JavaScript数组元素显示未定义
- 如何在.html段落中显示.js文件中的数组元素
- 如何动态显示json数组元素到选择标签
- 在jquery中显示数组元素
- 显示有延迟的数组元素
- 如何在 jQuery 的输入字段中使用空格键逐个显示数组元素
- 不要显示数组元素