Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angularjs - How can I display HTML in a <div> with Angular 1.2.0 - rc2

I am trying to display some HTML in my web page and using the following:

        xx {{ pageHtml }} yy 

        <div data-ng-bind-html-unsafe="$scope.pageHtml"></div>

The data between xx and yy shows up as raw HTML but what I want is to not show it as raw. I used the code on the second line but nothing shows.

Is there something I am missing? Did something change in 1.2 because I thought this was working before?

Update - I do 100% trust the HTML and don't want to clean it. There will be code inside the HTML that needs to show on the screen.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By default the innerHTML-ed expression result is sanitized using the $sanitize service which would require you to include ngSanitize in your module's dependencies.

<div data-ng-bind-html="pageHtml"></div>

However if you trust the HTML to be safe, you can bypass sanitization using $sce service that you would inject in your controller:

$scope.someSafeContent = $sce.trustAsHtml("<i>Hello</i> <b>World!</b>");

HTML:

<!-- bypasses sanitizaton -->
<div data-ng-bind-html="someSafeContent"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...