[英]Use ng-show to replace a character in a string
問題:我希望能夠替換& 與&一起使用ng-show。
因此,我有一個角表達式“ e.Example”,其中包含以下字符串{Camp&Stuff,Stuff $ Camp}。 當我在表單中生成字符串時,它將“&”表示為“&”。 等等。
我想做類似的事情:
<span ng-show="e.Example ==='$' replace it with someting else"></span>
如何使用ng-show達到目標? 我知道在C#中我可以使用.Contains和.Replace。 我想知道它們是否與angularjs類似
@蘭薩娜
我嘗試了以下方法:
<span ng-show="e.Specialty === '&' && doSomethingTo(e.Specialty)"></span>
但是,當我查看調試器時,它顯示的是&char而不是'&amp;'。 單擊求和並顯示結果時出現的“&”
更新:嘗試以下操作:
<span ng-show="doSomethingTo()"></span>
function doSomethingTo(val){
val.property = '&';
return true;
}
使用雙“&”號比較運算符。 如果第一個語句為true,則將執行第二個語句。
像這樣:
<span ng-show="e.Example ==='$' && doSomethingTo(e.Example)"></span>
function doSomethingTo(val) {
val.property = 'foo';
return true;
}
在此處查看此代碼段。
看看在將ng-bind-html
與$sce.trustAsHtml
函數一起使用時,是否做錯了什么。
(function() { angular .module('app', []) .controller('TestController', function($sce, $scope) { // Inject $sce service and call trust as html function $scope.test = $sce.trustAsHtml("Camp & Stuff, Stuff $ Camp"); }); })();
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> <title>Test</title> </head> <body ng-app="app"> <div ng-controller="TestController as ctrl"> <!-- Add ng-bind-html with the variable that contains the ampersand problem --> <span ng-bind-html="test"></span> </div> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.