angular.js - angularjs处理/n转<br/>时候 <br/>不会解析的问题
仅有的幸福
仅有的幸福 2017-05-15 16:52:37
0
3
637
<!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
仅有的幸福
仅有的幸福

reply all(3)
刘奇

You need to use 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>
黄舟

The reason for non-parsing is that angularjs filters the HTML and changes the < > symbol into & l t; & g t;, as evidenced by the picture. I checked and it turns out that the filter can be disabled. I'm really not familiar with angularjs, so I can't help you.

Peter_Zhu
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/>')

Tweet it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template