angular.js - angularjs处理/n转<br/>时候 <br/>不会解析的问题
问题描述
<!DOCTYPE html><html ng-app><head lang='en'> <meta charset='UTF-8'> <title></title> <script src='https://www.haobala.com/wenda/angular.min.js'></script> <script>function TextareaCtrl($scope){ var str='啦啦11范德萨范德萨nfadsfadsfadnfdfadfanfdafa'; $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
问题解答
回答1:要用到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范德萨范德萨nfadsfadsfadnfdfadfanfdafa'; $scope.name=$sce.trustAsHtml(str.replace(/n/g,'<br/>')); }]); </script></body></html>回答2:
造成不解析的原因是angularjs对html进行了过滤,把< > 符号变为 & l t; & g t;,有图为证。我查了一下是可以禁用过滤器的,angularjs 实在不熟悉,帮不上你。
scope.Datas.userInfo.rich_summary=scope.Datas.userInfo.rich_summary.replace(/rn/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/>’)
转一下
相关文章:
1. win10 python3.5 matplotlib使用报错2. 修改my.cnf后mysql无法运行???3. python怎么在字符串中标记子字符串的位置4. python - 图片爬虫时候遇到问题 urllib.request.urlretrieve 下载到指定文件夹不成功?5. 利用百度地图API定位及附件商家信息服务6. css - body 的 background 位置7. 怎么能做出标签切换页的效果,(文字内容随动)8. (python)关于如何做到按win+R再输入文件文件名就可以运行?9. css如何实现图标和文字的“绝对对齐”呢?10. javascript - vue2如何动态生成checkbox默认选中,获取数据

网公网安备