search

Home  >  Q&A  >  body text

angular.js - angularjs处理/n转<br/>时候 <br/>不会解析的问题

<!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
仅有的幸福仅有的幸福2744 days ago636

reply all(3)I'll reply

  • 怪我咯

    怪我咯2017-05-15 16:54:37

    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>
    

    reply
    0
  • 黄舟

    黄舟2017-05-15 16:54:37

    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.

    reply
    0
  • 天蓬老师

    天蓬老师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/>')
    

    Tweet it

    reply
    0
  • Cancelreply