2016-08-02 123 views
0

我是新手前端开发人员,我试图找出利用每个逗号后都有一个新行的文本区域的最佳方法。我真的试图找出最好的办法是在AngularJs什么(过滤器,正则表达式等)Textarea以逗号后的新行Angularjs

现在:

hello,how,are,you 

后:

Hello 
how 
are 
you 

当前:

<textarea cols="105" rows="30" disabled="true" 
      style="background-color: floralwhite; resize: none; white-space: normal; margin-top: 0px; border: none;"> 
    {{profile.exampleValues}} 
</textarea> 

我试着写,没有运气的过滤器:

.filter('newlines', function() { 
    return function (text) { 
     return text.replace(',', '<br/>'); 
    } 
}) 
+1

新行是'\ n','
'是一个HTML标记。 –

回答

1

我更新了你的代码得到它的工作。

首先,white-space: normal;通过white-space: pre-line;

其次替换中,过滤器进行了改进,以应付正则表达式。根据W3C

的替换()方法检索字符串为一个规定值,或正则表达式,并返回其中指定的值置换的新字符串。

注意:如果要替换值(而不是正则表达式),则只会替换该值的第一个实例。要更换指定的值的所有事件,使用全局(G)修改

app.filter('newlines', function() { 
    return function (text) { 
    return text.replace(/,/g, '\n'); 
    }; 
}); 

其结果是,在textarea应该是这样的:

<textarea cols="105" rows="30" disabled="true" 
      style="background-color: floralwhite; white-space: pre-line; resize: none; margin-top: 0px; border: none;"> 
    {{profile.exampleValues | newlines}} 
</textarea> 

请看Plunker