[英]Word Wrapping using ng-bind-html
I am using AngularJS to build a personal note management application. 我正在使用AngularJS构建个人笔记管理应用程序。 During so, I ran into a few problems where I get to choose my bug: Whether I should have an application that can't word wrap or one that can't sense the return key.
在此期间,我遇到了一些问题,我不得不选择我的错误:是我应该拥有一个不能自动换行的应用程序还是一个无法感知返回键的应用程序。
At first, my app wouldn't sense the return key or extra spaces, so, in my note directive, I replaced: <p>{{note.body}}</p>
with: <p ng-bind-html="note.body | format"></p>
起初,我的应用程序不会感知返回键或多余的空格,因此,在我的note指令中,我将
<p>{{note.body}}</p>
替换为: <p ng-bind-html="note.body | format"></p>
While it fixed the issue with the enter key, not it wouldn't word wrap: 虽然它用Enter键解决了问题,但它不会自动换行:
Using ng-bind-html, automatic word wrapping fails - which would be horrible for my app, since I want it to be extremely flexible when it comes to resizing. 使用ng-bind-html时,自动自动换行会失败-这对于我的应用程序来说太可怕了,因为我希望它在调整大小时非常灵活。
How to I fix this error? 如何解决此错误?
Here's my format filter: angular.module('NoteWrangler') .filter('format', function (){ return function(input) { if(!input) return input; var output = input //replace possible line breaks. .replace(/(\\r\\n|\\r|\\n)/g, '<br/>') //replace tabs .replace(/\\t/g, ' ') //replace spaces. .replace(/ /g, ' '); return output; }; });
这是我的格式过滤器:
angular.module('NoteWrangler') .filter('format', function (){ return function(input) { if(!input) return input; var output = input //replace possible line breaks. .replace(/(\\r\\n|\\r|\\n)/g, '<br/>') //replace tabs .replace(/\\t/g, ' ') //replace spaces. .replace(/ /g, ' '); return output; }; });
Note Directive: 注意指令:
```<div class="container note note-full">
<h2>{{note.title}}</h2>
<p ng-bind-html="note.body | format"></p>
<p class="text-muted date">Posted on {{note.timestamp | date}}</p>
</div>```
If using the tag would be the only way to solve this, then I'll do it - after I make the background, border, etc. invisible w/ some CSS. 如果使用标签是解决此问题的唯一方法,那么我将做-在使背景,边框等不可见(带有一些CSS)之后。
Add this css class to your <p>
or whatever tag you want to use : 将此CSS类添加到
<p>
或您要使用的任何标签中:
.text-pre-wrap{
white-space: pre-wrap !important;
}
Plus you only need this as your filter 另外,您只需要这个作为过滤器
angular.module('NoteWrangler').filter('format', function () {
return function (input) {
if (input) {
return input.replace(/\n/g, "<br />");
}
};
});
Adding the property 添加属性
.text-pre-wrap{
white-space: pre-wrap !important;
}
was not enough for me. 对我来说还不够。
I also needed to add: 我还需要添加:
.text-pre-wrap{
white-space: pre-wrap !important;
overflow-wrap: break-word;
}
And in the html: 并在html中:
<div ng-bind-html="notes.html" class="text-pre-wrap"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.