因为我要做的界面和组长的界面差不多所以我直接拷贝的代码,本来是好好的、安安静静地在那里展示着,但是不知道怎么回事,全乱了,所以调样式吧,后台传给我的是一张试卷的json?,接到之后转格式
改之后的代码,这样展示的是一行,将 什么的都去掉了:
ngOnInit() {
if (this.question.stuscore == "" || this.question.stuscore == null) {
this.question.stuscore = "0"
}
if (this.question.studentAnswer.includes("<br>")) {
this.changeWordForShow(this.question.studentAnswer);
}
}
changeWordForShow(studentAnswerData: string) {
let arg: string[] = this.question.studentAnswer.split("<br>");
let textForAnswer: string = "";
for (var i = 0; i < arg.length; i++) {
textForAnswer = textForAnswer + arg[i].toString();
}
this.question.studentAnswer=textForAnswer;
let lastThingForAnswer: string = '';
if (textForAnswer.includes(" ")) {
let dataMerge:any = this.question.studentAnswer.split(" ");
dataMerge.forEach(eleIndex => {
lastThingForAnswer = lastThingForAnswer + eleIndex.toString();
});
this.question.studentAnswer = lastThingForAnswer;
} else {
this.question.studentAnswer = textForAnswer;
}
}
之前组长的代码,这样是多行展示的:对用户来说更好看一点,但是不起作用、诶
ngOnInit() {
this.questionAnswer = this.turnAnswer(this.question.answer);
this.studentAnswer = this.turnAnswer(this.question.studentAnswer);
}
具体的操作
//多个答案间 会有 | 这样就不是 json 了,转json会报错 所以先分割成数组在一次转格式
turnAnswer(answer) {
if (answer != null && answer.length > 0) {
if (answer.includes("|")) {
let echoBusinessList: Array<string> = answer.split("|");
// private questionSubList = new Array<QuestionSubEntity>(); //页面上显示的答案
// for (let i = 0; i < echoBusinessList.length; i++) {
// this.questionSubList.push({ optionOrder: i + 1,optionsContent: '',questionMainId: '' });
// }
let dataMsg: any="";
echoBusinessList.forEach(x => {
dataMsg = dataMsg + this.processAnswer(JSON.parse(x));
});
return dataMsg;
} else {
return this.processAnswer(JSON.parse(answer));
}
}
}
/* 处理答案 */
private processAnswer(businessList: BussinessModel[]) {
let answerForShow: string = "";
businessList.forEach((element,index) => { answerForShow += "业务" + (index + 1) + " : " + element.explain + "<br>"; answerForShow += this.joinAnswer(element.borrowList); answerForShow += this.joinAnswer(element.loanList); answerForShow += "<br>" }) return answerForShow; //这个是因为上面的不起作用, 就只有显出来了,我这里不起作用,我们组长那里貌似好好的,悲伤~~ // let answerForShow: string = ""; // businessList.forEach((element,index) => { // answerForShow += "业务" + (index + 1) + " :" + element.explain + " "; // answerForShow += this.joinAnswer(element.borrowList); // answerForShow += this.joinAnswer(element.loanList); // answerForShow += " ;" // }) // return answerForShow; }
连接答案
/* 连接答案 */
private joinAnswer(item) {
let answer: string = "";
item.forEach(element => {
answer += " " + element.type + " " + element.subject
+ " " + element.explain + " " + element.amount + "<br>";
})
return answer;
//replace只对第一个起作用,这个也是试了很多次才知道的
// let answer: string = "";
// item.forEach((element,index) => {
// let loanlistData = element.type.split();
// element.type = element.type.replace(" ","");
// element.type = element.type.replace(" ","");
// answer += " " + element.type + "" + element.subject
// + " :" + element.explain + "" + element.amount + " ";
// })
// return answer;
}
原文链接:https://www.f2er.com/angularjs/144937.html