Angular 2单击带有空格的特定单词

时间:2017-06-16 22:04:49

标签: javascript angular typescript input

我有文字,文字中的一些单词带有空格,例如我喜欢R O S E S和树木。空间是借助此功能制作的

this.text = this.text.replace(new RegExp(this.words.join('|'), 'g'), function insertSpaces(x) {
   return x.split('').join(' ');
 }); };

文字我喜欢玫瑰和树木而字词 ROSES ,我想点击 ROSES 这个词在字母之间,如果我点击那里出现“ - ”而不是空格。因此,如果我在 R O 之间点击,则会出现 RO SES 而不是 ROSES ,如果我点击 O S ,出现 R OS ES 而不是 ROSES 等等......是否可以使用Angular2?

更新

我现在尝试这个代码

getCaretPos(oField: any, a: any) {
    if (oField.selectionStart || oField.selectionStart === '0') {
      this.caretPos = oField.selectionStart;

//assign char at choosed position to new variable
      a = this.text.charAt(this.caretPos);

//replace this char with "-"
      this.text.replace(a, '-');
    }
  }
}

2 个答案:

答案 0 :(得分:1)

此链接演示了如何获取当前插入位置:http://blog.sodhanalibrary.com/2016/10/get-cursor-position-from-text-area.html#.WURW3GjythE

然后,您可以更改文本以在该字符位置插入短划线。

这听起来对你有用吗?

答案 1 :(得分:0)

getCaretPos(oField: any, a: any) {
    if (oField.selectionStart || oField.selectionStart === '0') {
      this.caretPos = oField.selectionStart;
      a = this.text.charAt(this.caretPos);
      alert(a);
      this.text = this.text.substring(0, this.caretPos) + '-' + this.text.substring(this.caretPos + 1);
    }
  }

适合我,希望这能帮助别人)