简体   繁体   English

无法使用角度2+在文本区域中添加行号和错误状态

[英]Unable to add line numbers and error status in text area using angular 2+

here i am facing a typical issue i have a Dynamic json data which is 在这里我面临一个典型的问题,我有一个动态json数据,

[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
]

And i have 2 text areas . 我有2个文本区域。 In text area One i try to paste this json data and after click action i am getting data transferred into 2nd text area here everything works good but the issue is i unable to set the line number programmatic for the text area by using css 在文本区域One中,我尝试粘贴此json数据,并在单击操作后将数据传输到第二个文本区域,这里一切正常,但问题是我无法通过使用CSS设置文本区域的行号

.textarea {
    background: url(http://i.imgur.com/2cOaJ.png);
    background-attachment: local;
    background-repeat: no-repeat;
    padding-left: 35px;
    padding-top: 10px;
    border-color:#ccc;
}

i am setting the line number the problem with this approach is suppose if i get any error in particular line how can i highlight it. 我正在设置行号,这种方法的问题是,如果我在特定行中遇到任何错误,该如何突出显示它。

now my issues are How can i add line numbers other than css way and how can show error in particular line if any i mean if i got error in line 5 & 10 then how can i high light those line 现在我的问题是我如何添加除CSS方式以外的行号以及如何显示特定行中的错误(如果有的话),如果我的意思是如果我在第5行和第10行中出现错误,那么我该如何突出显示这些行

below is my stack blitz https://stackblitz.com/edit/angular-zgyxet 下面是我的堆栈闪电战https://stackblitz.com/edit/angular-zgyxet

在此处输入图片说明

Unfortunately you can't change the css properties of certain lines of text in a textarea, but you can simulate a textarea by using the contenteditable attribute on a div and editing the div's css accordingly. 不幸的是,您不能更改textarea中某些文本行的css属性,但是可以通过在div上使用contenteditable属性并相应地编辑div的css来模拟文本区域。

My approach to the solution was to split the inputted text by line break into an array of strings, adding a span around each line that results in an error, then joining the array back into a string. 我解决方案的方法是通过换行符将输入的文本拆分为一个字符串数组,在每行周围添加一个跨度,这会导致错误,然后将该数组重新组合为字符串。

Here's a StackBlitz showing my quick fix: https://angular-zclmnm.stackblitz.io 这是一个显示我的快速修复的StackBlitz: https ://angular-zclmnm.stackblitz.io

HTML 的HTML

<div [innerHTML]="TextData1" contenteditable></div>

TS (note I removed some unneeded variables) TS (请注意,我删除了一些不需要的变量)

test(){
    var tempData = [];
    this.TextData1 = this.TextData.split("\n");                  // split TextData on line break

    this.TextData1.map(data => {
      if(error === true){                                        // Set error conditions
        data = data.replace(/\s+/g, '').trim();
        data = "<span class='highlight'>" + data + "</span>"     // Add class to error row
        tempData.push(data);
      } else{
        tempData.push(data);
      }
    });
    this.TextData1 = tempData.join('');
  }

note: don't forget to add encapsulation: ViewEncapsulation.None to the @Component decorator 注意:不要忘记添加encapsulation: ViewEncapsulation.None@Component装饰器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM