如何以角度动态添加问题和答案? [英] How dynamically add questions and answers in angular?
问题描述
您好我有json数据字典在那个问题id是映射到问题对象,问题对象包含可能的答案数组,其中包含是否,对于是答案我在字典中有下一个问题ID而对于no则相同。我想在选择第一个问题答案后动态绘制这个问题,然后添加下一个问题。所以我怎么做如果yesid和noid包含-1,我想继续下一个问题,而不用担心以前的问题答案。
以下是我的json
{
1:{
类别:第一,
Qid:1,
问题:是你年满18岁?,
PossibleAnswers:{
1:是,
2:否
},
YesID:2,
NoID:3
},
2:{
类别:秒,
Qid:2,
问题:你毕业了吗?,
PossibleAnswers:{
1:是,>
2:否
},
YesID: - 1,
NoID: - 1
}
}
我的尝试:
我尝试过ng-repeat和ng-show和ng-hide但是我无法获得最终输出。
<!DOCTYPE html>
< html>
< head>
< script src =http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js>< / script>
< script src =testController.js>< / script>
< / head>
< div ng-app =myAppng-controller =testCtrl>
< li ng-repeat =QaAns in quesans>
< ul ng-hide =QaAns.1.YesID == -1> {{QaAns.1.Category}} - {{QaAns.1.Qid}}。{{QaAns.1.Question}}< / ul>
< ul ng-hide =QaAns.2.YesID == -1> {{QaAns.2.Category}} - {{QaAns.2.Qid}}。{{QaAns.2.Question}}< / ul>
< ul ng-hide =QaAns.3.YesID == -1> {{QaAns.3.Category}} - {{QaAns.3.Qid}}。{{QaAns.3.Question}}< / ul>
< / li>
< / div>
< / html>
testController.js
angular.module('myApp',[])。controller('testCtrl',function(
scope){
scope.quesans = [
{1:{Category:First,Qid:1,Question:你在18岁以上吗?,
PossibleAnswers:{1:是,2:否},YesID:2,NoID:3
},
2:{ 类别:第二,Qid:2,问题:你毕业了吗?,
PossibleAnswers:{1:是,2:否} ,YesID: - 1,NoID: - 1
},
3:{类别:第三,Qid:3,问题:移至问题3 ..有用吗?,
PossibleAnswers:{1:是,2:否},YesID:1,NoID:1
}
}
];
});
输出:
首先 - 1.你是否超过18岁?
第三 - 3.moved to问题3 ..有用吗?
Hi I am having json data dictionary in that question id is map to question object and question object contains probable answers array which contains yes no and for yes answer i am having next question id in dictionary and same for no. And i want to plot this dynamically after first question answer selected next questions will be added. so how i do this. and if yesid and noid contains -1 i want to go for next question wihout worrying about previous question answers.
below is my json
{
"1": {
"Category": "First",
"Qid": 1,
"Question": "Are you above 18 yr's ?",
"PossibleAnswers": {
"1": "Yes",
"2": "No"
},
"YesID": 2,
"NoID": 3
},
"2": {
"Category": "second",
"Qid": 2,
"Question": "Are you graduate?",
"PossibleAnswers": {
"1": "Yes",
"2": "No"
},
"YesID": -1,
"NoID": -1
}
}
What I have tried:
I am tried ng-repeat and ng-show and ng-hide but i am unable to get final output.
Index.html
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script> <script src="testController.js"></script> </head> <div ng-app="myApp" ng-controller="testCtrl"> <li ng-repeat="QaAns in quesans"> <ul ng-hide ="QaAns.1.YesID == -1"> {{QaAns.1.Category}} - {{QaAns.1.Qid}}.{{QaAns.1.Question}}</ul> <ul ng-hide ="QaAns.2.YesID == -1"> {{QaAns.2.Category}} - {{QaAns.2.Qid}}.{{QaAns.2.Question}}</ul> <ul ng-hide ="QaAns.3.YesID == -1"> {{QaAns.3.Category}} - {{QaAns.3.Qid}}.{{QaAns.3.Question}}</ul> </li> </div> </html>
testController.js
angular.module('myApp', []).controller('testCtrl', function(
scope) {
scope.quesans = [ { "1": {"Category": "First","Qid": 1,"Question": "Are you above 18 yr's ?", "PossibleAnswers": { "1": "Yes", "2": "No" }, "YesID": 2, "NoID": 3 }, "2": {"Category": "Second","Qid": 2,"Question": "Are you graduate?", "PossibleAnswers": { "1": "Yes", "2": "No" }, "YesID": -1, "NoID": -1 }, "3": {"Category": "Third","Qid": 3,"Question": "moved to question 3.. Is it helpful?", "PossibleAnswers": { "1": "Yes", "2": "No" }, "YesID": 1, "NoID": 1 } } ]; });
Output:
First - 1.Are you above 18 yr's ?
Third - 3.moved to question 3.. Is it helpful?
这篇关于如何以角度动态添加问题和答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!