<!DOCTYPE html>
<html ng-app>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="angular.min.js"></script>
<script>
function TextareaCtrl($scope)
{
var str="啦啦11范德萨范德萨\nfadsfadsfad\nfdfadfa\nfdafa";
$scope.name=str.replace(/\n/g,"<br/>");
}
</script>
</head>
<body>
<p ng-controller="TextareaCtrl">
<p>{{name}}</p>
</p>
</body>
</html>
結果:
啦啦11范德萨范德萨<br/>fadsfadsfad<br/>fdfadfa<br/>fdafa
怪我咯2017-05-15 16:54:37
要用ng-bind-html
<!DOCTYPE html>
<html ng-app="test">
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p ng-controller="TextareaCtrl">
<p ng-bind-html="name"></p>
</p>
<script src="http://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js"></script>
<script>
var myModule = angular.module("test",[]);
myModule.controller("TextareaCtrl",["$scope","$sce",function($scope,$sce){
var str="啦啦11范德萨范德萨\nfadsfadsfad\nfdfadfa\nfdafa";
$scope.name=$sce.trustAsHtml(str.replace(/\n/g,"<br/>"));
}]);
</script>
</body>
</html>
黄舟2017-05-15 16:54:37
造成不解析的原因是angularjs對html進行了過濾,把< > 符號變為 & l t; & g t;,有圖為證。我查了一下是可以停用濾鏡的,angularjs 實在不熟悉,幫不上你。
天蓬老师2017-05-15 16:54:37
scope.Datas.userInfo.rich_summary=scope.Datas.userInfo.rich_summary.replace(/\r\n/gi,'<br/>')
scope.Datas.userInfo.rich_summary=scope.Datas.userInfo.rich_summary.replace(/\r/gi,'<br/>')
scope.Datas.userInfo.rich_summary=scope.Datas.userInfo.rich_summary.replace(/\n/gi,'<br/>')
轉一下