angularjs手动识别字符串中的换行符

  • Post author:
  • Post category:其他



情景一


html:



<textarea


style=”text-align: left;color: yellow;” disabled=”true”>{

{


value

}}</textarea>




controller:



$scope




.


value


=”1.javaScript \n 2.html5 \n 3.C++”;


显示:



1.javaScript



2.html5



3.C++


情景二


html:



<textarea


style=”text-align: left;color: yellow;” disabled=”true”>{

{


value

}}</textarea>




controller:



$scope


.

value

=

$scope

.resource.

valuestr

; //从strings_en.properties文件中读取字符串



strings_en.properties:





valuestr=

1.javaScript \n 2.html5 \n 3.C++






显示:







1.javaScript \n 2.html5 \n 3.C++







可见,这种情况下,<

textarea

>并没有识别’\n’换行符















解决办法:







function newLineBySign(value){




value=trim(value);



var result=”;



if(value.indexOf(“\\n”)>0) {



result=value.replace(/(\\n)/g,”$1\n”);



result=result.replace(/(\\n)/g,””);



}



return result;

}









controller:











$scope


.value=

newLineBySign(

$scope.resource.valuestr); //手动识别换行符













显示:



1.javaScript



2.html5



3.C++





结束语:实际应用中,为了解决国际化问题,我们都会将字符串资源放到指定的配置文件中,比如我用到的








strings_en.properties和

strings_zh.properties 分别表示英文和中文。从文件中获取字符串资源后html控件并












不能自动识别换行符,此时便需要我们手动去识别。







版权声明:本文为baidu_26611019原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。