将所有↵替换为角度循环中的新线 [英] Replace all ↵ for a new line in angular loop

查看:60
本文介绍了将所有↵替换为角度循环中的新线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序当前位于以下位置有一个循环:

I have a loop currently in my application located here:

<tr ng-repeat="audit in audctrl.audits | orderBy:'-created_at'">
  <td>
    {{audit.objects}}
  </td>
</tr>

对象的代码显示很多文本,但是带有很多↵符号.

The object's code shows a lot of text, but with a lot of the ↵ symbol.

我该如何替换这些?

我尝试过

{{audit.object.replace(/\u21B5/g,'<br/>')}}

使这项工作最好的方法是什么?

What would be the best method to make this work?

推荐答案

我建议您编写一个角度过滤器.例如:

I would suggest you to write an angular filter. For example:

myApp.filter('multiline', function () {
    return function(text) {
        return text.replace(/\n/g, '<br>');
    }
});

然后用管道调用它:

<tr ng-repeat="audit in audctrl.audits | orderBy:'-created_at'">
  <td>
    {{audit.objects | multiline}}
  </td>
</tr>

@编辑我不确定您的新行字符代码,但是您可以轻松对其进行更改.角度过滤器很好,因为它们可以在应用程序的其他部分中重复使用.

@Edit I'm not sure about your new line character code but you can easy change it. Angular filters are good because they are reusable in other parts of application.

@ Edit2要在Angular绑定中显示html标签,您必须使用 $ sce 服务和 ng-bind-html ="audit.objects | multiline" 而不是 {{audit.objects |多行}}

@Edit2 To display html tags in Angular binding u have to use $sce service and ng-bind-html="audit.objects | multiline" instead of {{audit.objects | multiline}}

http://jsfiddle.net/c1qwg776/

这篇关于将所有↵替换为角度循环中的新线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆