最近在解决一个问题,就是这个
在界面回显的时候会出现 的样式
这是因为在MySQL中,空格是用 表示的,所以在回显的过程中,HTML界面就会显示的这个了。
那么怎么去掉他呢?
我的解决方法是直接在前台进行转义,通过扫描字符串,替换实现的。
部分代码:
ngOnInit() {
if (this.question.writedAnswerList != null && this.question.writedAnswerList.length != 0) {
this.userText = this.question.writedAnswerList[0].replace(/<br\/>/g, '\n').replace(/ /g, ' ');
}
}
onKeyPress(event: any) {
// 转换为字符串数组进行处理
const saveTextChar: string[] = this.userText.split('');
for (let i = 0; i < saveTextChar.length; i++) {
// aa[len+1-i]=aa[len-i];
if (saveTextChar[i] == ' ') {
saveTextChar[i] = ' ';
}
if (saveTextChar[i] == '\n') {
saveTextChar[i] = '<br/>';
}
}
// 更新存储文本
this.saveText = '';
for (let i = 0; i < saveTextChar.length; i++) {
this.saveText = this.saveText + saveTextChar[i];
}