2017-02-02 18 views
14

Kiedy próbowałem pokazać moje dane jako tekst-html, to wyświetlane w formacie HTML, ale kiedy odświeżone strony, otrzymuję ten błąd:AngularJS: Jak rozwiązać problem "Próba użycia niebezpiecznej wartości w bezpiecznym kontekście"?

[$sce:unsafe] Attempting to use an unsafe value in a safe context.

Oto moje angularjs Kod:

data.attributes.task_name = $sce.trustAsHtml(data.attributes.task_name); 

HTML

<span ng-bind-html="taskdata.attributes.task_name" data-html="true" title="{{reminder.attributes.message}}"></span> 
+0

Czy [moja odpowiedź] (http://stackoverflow.com/a/41997335/4927984) rozwiązała Twój problem? – Mistalis

Odpowiedz

31

z Angular documentation:

The value provided for use in a specific context was not found to be safe/trusted for use.

AngularJS's Strict Contextual Escaping (SCE) mode (enabled by default), requires bindings in certain contexts to result in a value that is trusted as safe for use in such a context. (e.g. loading an AngularJS template from a URL requires that the URL is one considered safe for loading resources.)

This helps prevent XSS and other security issues. Read more at Strict Contextual Escaping (SCE)

You may want to include the ngSanitize module to use the automatic sanitizing.


Trzeba to ngSanitize:

załadować go na index.html:

<script src="lib/angular/angular-sanitize.min.js"></script> 

wstrzyknąć go jako zależność w swojej aplikacji . js:

angular.module('myApp', ['...', 'ngSanitize']); 
+0

To sprawiło, że cuda pozwoliły mi podzielić się postacią z liniowym łamaniem w AngularJS. Dziękuję Ci! – wickedchild