2016-12-05 33 views
0

我有一個字符串,其中有很多",我用一個空字符串替換它,並且工作正常。 但是我也想在逗號出現時換行。在uib-alert中用字符串中的換行符替換逗號工作

已經嘗試過('\n'),('\r'),('<br>'),('<br/>'),但沒有任何效果。 在我的角度控制器我有串

self.msg = msg.replace(/"/gi, '').replace(/,/gi, '\n'); 
self.testAlerts = [{ type: 'success', msg: self.msg}]; 

我想在我的HTML顯示的警告框,此消息

<div uib-alert ng-repeat="alert in testAlerts" type="{{alert.type}}" >{{alert.msg}}</div> 

爲什麼換行不行?

回答

1

使用NG綁定-HTML而不是插值:

<div uib-alert ng-repeat="alert in testAlerts" type="{{alert.type}}" ng-bind-html="alert.msg"></div> 

下面是一個示範plunkr

+0

這給我的錯誤:「錯誤:[$ SCE:不安全]。在嘗試一個安全的前提下使用 – nolags

+1

$ sce.trustAsHtml在你的控制器代碼中使用不安全的值我更新了[plunkr(HTTPS: //plnkr.co/edit/7Y1V0bp8Uru7raQ5cBhf?p=preview) – RamblinRose

+0

真的太棒了!謝謝。 – nolags

0

如果從角世界上任何被碰巧在這裏你可以使用 [innerHTML的]而不是[NG-綁定-HTML]從@ RamblinRose的回答

甚至,只需添加CSS的白色空間類

<div uib-alert ng-repeat="alert in testAlerts" style="white-space: pre-line;" type="{{alert.type}}" >{{alert.msg}}</div> 
相關問題