programing

html 문자열을 html로 인쇄하는 방법

newnotes 2023. 3. 31. 22:37
반응형

html 문자열을 html로 인쇄하는 방법

예를 들어 다음과 같은 경우:

var = "<a>Asd</a>";

<span>{{ var }}</span>

문자열은 html이 아닌 텍스트로 출력됩니다만, html은 어떻게 인쇄합니까?

지시문을 사용해야 합니다.

내부 바인딩을 만듭니다.표현식을 현재 요소로 안전한 방법으로 평가한 결과 HTML.

<ANY ng-bind-html="{expression}">
   ...
</ANY>

ng-bind-html 디렉티브를 사용하기 전에 $sanitize 서비스를 포함해야 합니다.그렇지 않으면 오류가 발생합니다.

오류: $sce:unsafe 안전/신뢰할 수 있는 값이 필요합니다. 안전한 컨텍스트에서 안전하지 않은 값을 사용하려고 시도 중입니다.

Error: [$sce:unsafe] http://errors.angularjs.org/1.4.5/$sce/unsafe
    at Error (native)

올바른 방법:

<script src="angular.js"></script>
<script src="angular-sanitize.js"></script>
var myApp = angular.module('app', ['ngSanitize']);
myApp.controller('MyController', ['$scope', function($scope) {
  $scope.myHTML = '<a href="#">Hello, World!</a>';
}]);
<div ng-controller="MyController">
 <p ng-bind-html="myHTML"></p>
</div>

https://docs.angularjs.org/api/ngSanitize

https://docs.angularjs.org/api/ng/directive/ngBindHtml

다음과 같은 작업을 수행할 수도 있습니다.


app.filter "to_filter" , ['$sce', function"sce) {반환 함수(텍스트) {$sce.trustAsHtml(텍스트)을 반환합니다.
};}]);

그 다음, 시야에서:


ng-syslog-syslog=" myHTML | to_syslog"

언급URL : https://stackoverflow.com/questions/21231630/how-to-print-html-string-as-html

반응형