angularjs手動識別字符串中的換行符

情景一

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控件並

不能自動識別換行符,此時便需要我們手動去識別。

發佈了30 篇原創文章 · 獲贊 21 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章