2013-12-11 14 views
5

html:

<div ng-app="app"> 
    <test-d class="my-class">this should be replaced</test-d> 
</div> 

JS:

angular.module('app', []).directive('testD', ['$compile','$sce', function($compile, $sce) { 
     return { 
      restrict: 'E', 
      link: function(scope, element, attrs, controller) { 

       //"case 1" 
       //var testElement = angular.element('<div class="{{testClass}}"><div ng-repeat="item in items">{{item.n}}.{{item.label}}</div></div>'); 

       //"case 2" 
       var testElement = angular.element('<div class="{{testClass}}"><div ng-repeat="item in items">{{item.n}}<div class="how-to-remove-this-div" ng-bind-html="item.label|htmlize"></div></div></div>'); 

       scope.testClass = attrs.class; 

       scope.items = [ 
        //{n:10,label:$sce.trustAsHtml('<span class="with-icon">label 11</span>')}, 
        // "case 1" of "testElement": no any effects (with "case 1" that would be preferred) 
        // "case 2" of "testElement": Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html 

        {n:20,label:'<span class="with-icon">label 22</span>'}, 
        {n:30,label:'<span class="with-icon">label 33</span>'} 
       ]; 

       var testElementCompiled = $compile(testElement)(scope); 

       //"case 3" 
       element.replaceWith(testElementCompiled); 

       //"case 4" 
       //element.replaceWith($sce.trustAsHtml(testElementCompiled)); 
      } 
     } 
}]).filter('htmlize', ['$sce', function($sce){ 
     return function(val) { 
      return $sce.trustAsHtml(val); 
     }; 
}]); 

jsfiddle: http://jsfiddle.net/isnigirev/c2wRq/

pytania: 1) jest możliwe aby użyć trustAsHtml z kontekstu filtra? 2) IF NIE jak pozbyć się div z "jak usunąć ten div" w przypadku korzystania z dyrektywy "ng-bind-html"?

+0

docs [tutaj] (http://docs.angularjs.org/api/ng.$sce) daj przykład użycia '$ sce' w dyrektywie, więc musi być możliwe użycie go poza filtrem – tennisgent

+0

Gotowy (trustAsHtml) działający w dyrektywie (http://jsfiddle.net/isnigirev/c2wRq/5/) . "Błąd: [$ sce: itype] Próba zaufania do wartości innej niż ciąg w treści wymagającej napisu: Context: html" Wystąpił błąd, gdy przez pomyłkę próbuję dwukrotnie wywołać trustAsHtml na tej samej "etykiecie". – ivsn

+0

Druga część pytania wciąż pozostaje, ale rozwiążę to z kilkoma obejściami. Poza tym warto to rozwiązać. – ivsn

Odpowiedz

11

Mam to (trustAsHtml) działające w dyrektywie here. Wystąpił błąd "Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html", gdy przez pomyłkę próbuję dwukrotnie wywołać trustAsHtml na tej samej "etykiecie".

Druga część pytania nadal pozostaje, ale rozwiążę go z kilkoma obejściami. Poza tym warto to rozwiązać.

4

Artykuł jest tekst, aby wyświetlić

<div ng-app="app"> 
<span data-ng-bind-html="article| to_trusted"></span> 
</div> 

i filtr jak:

app.filter('to_trusted', ['$sce', function($sce){ 
     return function(text) { 
      return $sce.trustAsHtml(text); 
     }; 
    }]); 
+1

Używam tego kodu i pokazuje on błąd: ** Błąd: $ sce: itype Wartość ciągu jest wymagana dla połączenia zaufania SCE ** jak go rozwiązać? – Ahmed

+0

trochę za późno, ale upewnij się, że przekazujesz ciąg znaków do 'trustAsHtml' - nie jest niezdefiniowany, nie ma wartości null, nie jest listą, ale łańcuchem. – blacklwhite

Powiązane problemy