【angular】ts擷取字串、json
阿新 • • 發佈:2018-12-31
因為我要做的介面和組長的介面差不多所以我直接拷貝的程式碼,本來是好好的、安安靜靜地在那裡展示著,但是不知道怎麼回事,全亂了,所以調樣式吧,後臺傳給我的是一張試卷的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(" ", "");
// element.type = element.type.replace(" ", "");
// element.type = element.type.replace(" ", "");
// element.type = element.type.replace(" ", "");
// element.type = element.type.replace(" ", "");
// answer += " " + element.type + "" + element.subject
// + " :" + element.explain + "" + element.amount + " ";
// })
// return answer;
}