2014-10-29 82 views
0

我有以下過濾器,似乎應該在我看來工作。當添加到指令中時,不會返回任何數據,就像數據中包含單引號一樣,就像「它是」一樣。角度過濾器取代單引號

angular.module('hcApp') 
.filter('replace', function(){ 
     return function(text) { 
      return text.replace(/'/g, '"'); 
      }; 
    }); 

我試圖逃避我的JSON數據,這是通過CMS來的單引號。這可能是與過濾器和指令已經存在的衝突嗎?

<div ng-bind-html="'{{over.contents}}' | to_trusted | replace "></div> 


angular.module('hcApp') 
.filter('to_trusted', ['$sce', function($sce){ 
    return function(text) { 
     return $sce.trustAsHtml(text); 
    }; 
}]); 
+0

過濾器是否正確地獲取輸入?嘗試將其打印到控制檯,例如。 '{{over.contents}}''在ng-bind-html裏面看起來不正確,因爲它需要一個表達式。您可以嘗試不使用「'和{{}}。 – Absor 2014-10-29 16:41:21

回答

1

您不需要插值('{{over.contents}}')。並且您希望to_trusted成爲最後一個過濾器,因爲它包裝字符串以告訴Angular它是可信的。試試這個......

<div ng-bind-html="over.contents | replace | to_trusted "></div> 

另外,&quot;是雙引號。對於單引號,請使用&#39;

Fiddle

+0

謝謝!這就說得通了 – byrdr 2014-10-29 17:04:48