Created
June 3, 2014 23:05
-
-
Save frontainer/54c159fb9b45488500f1 to your computer and use it in GitHub Desktop.
改行コードを<br>に変換するAngularJSフィルタ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 改行コードをbrに変換するフィルタ | |
| */ | |
| angular.module('filters').filter('lineBreak', function () { | |
| return function (input, exp) { | |
| return input.replace(/\n|\r/g, "<br>"); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AngularJS v1.3.13で実装したところ、サニタイズが効いていて
<br>がHTMLタグとして置換されなかったので、以下のように追記して使用しました。また、
<p ng-bind-html="sample | lineBreak"></p>のように、view側でngBindHtmlを併用する必要があります。また、上記のfilterだけでは
<br>以外のhtml要素が有効になってしまうので、さらに以下のようなソースコードにして使用しています。(この部分はもっとスマートな方法がある気もしています)