accordion with ng-repeat and ng-bind-html won't work
12,333
This is because the HTML content is declared unsafe by Angular due to it's Strict Contextual Escaping.
Another SO answer already explains clearly how this can be solved: HTML injection, that is if you are using Angular version 1.2.0 or up.
I created a Plunkr to match your case.
Author by
Jayyrus
I'm an Italian Software Engineer. I like very much to learn and study new technologies
Updated on June 13, 2022Comments
-
Jayyrus about 2 years
I'm trying to use accordion and html content in this way:
<accordion> <accordion-group ng-repeat="item in items"> <accordion-heading> <a class="btn btn-primary btn-block btn-elenco"> <img postsrc="img/flag/flag_{{item.index}}.jpg"> </a> </accordion-heading> <p ng-bind-html="item.content"></p> </accordion-group> </accordion>
AND
var items = []; for(var i=0;i<10;i++){ var content = "<div>TEST</div>"; items.push({index:i,content:content}); } $scope.items = items; var app = angular.module('MyApp',['ngSanitize','ui.bootstrap']);
Accordion works but html isn't rendered into p tag.
What could be the problem?
EDIT
If i try something like:
<div ng-bind-html="to_trusted(item.content)"></div>
And add function to controller:
$scope.to_trusted = function(html_code) { console.log(html_code); return $sce.trustAsHtml(html_code); }
Nothing changes and in console i get many "undefined"!
-
Marco van Dijk over 10 yearsWhat angular version do you use? I replicated your code in the Plunkr and it works. Can you perhaps construct a Plunkr with your code?
-
Jayyrus over 10 yearsi'm using AngularJS v1.2.9