【angular】ts截取字符串、json

前端之家收集整理的这篇文章主要介绍了【angular】ts截取字符串、json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

因为我要做的界面和组长的界面差不多所以我直接拷贝的代码,本来是好好的、安安静静地在那里展示着,但是不知道怎么回事,全乱了,所以调样式吧,后台传给我的是一张试卷的json?,接到之后转格式

改之后的代码,这样展示的是一行,将&nbsp什么的都去掉了:

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("&nbsp;&nbsp;")) {
      let dataMerge:any = this.question.studentAnswer.split("&nbsp;&nbsp;");
      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) + "&nbsp;&nbsp;:&nbsp;&nbsp;" + element.explain + "<br>"; answerForShow += this.joinAnswer(element.borrowList); answerForShow += this.joinAnswer(element.loanList); answerForShow += "<br>" }) return answerForShow; //这个是因为上面的不起作用,&nbsp;就只有显出来了,我这里不起作用,我们组长那里貌似好好的,悲伤~~ // 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 += "&nbsp;&nbsp;&nbsp;&nbsp;" + element.type + "&nbsp;&nbsp;&nbsp;&nbsp;" + element.subject
      + "&nbsp;&nbsp;&nbsp;&nbsp;" + element.explain + "&nbsp;&nbsp;&nbsp;&nbsp;" + element.amount + "<br>";
  })
  return answer;
  //replace只对第一个起作用,这个也是试了很多次才知道的
  // let answer: string = "";
  // item.forEach((element,index) => {
  //   let loanlistData = element.type.split();
  //   element.type = element.type.replace("&nbsp;","");
  //   element.type = element.type.replace("&nbsp;","");
  //   answer += " " + element.type + "" + element.subject
  //     + " :" + element.explain + "" + element.amount + " ";
  // })
  // return answer;
}
原文链接:https://www.f2er.com/angularjs/144937.html

猜你在找的Angularjs相关文章