반응형
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
반응형
'programing' 카테고리의 다른 글
| WordPress에서 .htaccess를 사용하여 후행 슬래시 제거 (0) | 2023.03.31 |
|---|---|
| Angular.js의 다이내믹 클래스 (0) | 2023.03.31 |
| 파라미터 변경 시 Angular Directive 새로 고침 (0) | 2023.03.31 |
| Angularjs: 여러 $resource URL/데이터 소스를 제공하는 서비스? (0) | 2023.03.31 |
| 도커: Apple Silicon M1의 Apache (0) | 2023.03.31 |