I want to render a list of widgets on a page from my controller. Each widget has its own render function that returns a safe HTML string.
My first part of my ng-repeat
(outside layer) looks like this:
$scope.renderWidgets = function() {
var html = "";
html += "<div ng-repeat='widget in widgets'>";
html += "<div ng-bind-html='widget.render()'></div>"
html += "</div>";
return $sce.trustAsHtml(html);
}
As you can see I have ng-bind-html
inside my ng-repeat
which is calling a function named .render()
located inside widget
:
this.render = function() {
var html = "";
html += "<div ng-repeat='choice in widget.choices'>";
html += "{{choice.name}}";
html += "</div>";
return $sce.trustAsHtml(html);
}
I also have a directive that I use to $compile
the above HTML string into AngularJS.
If I run the above code the widget.render()
function gets called just fine but the output on the page will look like this: {{choice.name}}
and not the value inside choice.name
.
As you can see I am trying to do another ng-repeat
inside ng-bind-html
with the widget
object from my first ng-repeat
.
Is this even possible what I am trying to do here (I am new to AngularJS)? If yes then what am I doing wrong? Or is there another way to resolve my problem?
I am using the latest AngularJS (v. 1.7.9).
UPDATE
I think I know the issue. When I call my first function $scope.renderWidgets()
the returned HTML will be automatically compiled(the first ng-repeat
) at which point the ng-bind-html
gets triggered returning a non-compiled HTML string. Now I have to figure out a way to $compile
the returned HTML from my ng-bind-html
directive.
Am I on the right track?