Skip to content

Instantly share code, notes, and snippets.

@izumin5210
Created April 20, 2014 03:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izumin5210/11104465 to your computer and use it in GitHub Desktop.
Save izumin5210/11104465 to your computer and use it in GitHub Desktop.
【AngularJS】改行を有効にするためのフィルタ
app = angular.module 'MyApplication'
app.filter 'noHTML', ->
(text) -> text.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/&/, '&amp;') if text?
app.filter 'newlines', ($sce) ->
(text) -> $sce.trustAsHtml(if text? then text.replace(/\n/g, '<br />') else '')
app.controller 'testCtrl', ($scope) ->
$scope.iroha = 'いろはにほへと ちりぬるを\nわかよたれそ  つねならむ\nうゐのおくやま けふこえて\nあさきゆめみし ゑひもせす'
<p ng-controller="testCtrl" ng-bind-html="iroha | noHTML | newlines"></p>
@ukyo
Copy link

ukyo commented Jun 18, 2014

4行目がこれな気がしますよ。

(text) -> text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') if text?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment