I've looked a LOT for this, but I either I can't find the answer or I don't understand it. A specific example will win the vote =)
- I have a function that returns an HTML string.
- I can't change the function.
- I want the html represented by the string to be inserted into the DOM.
- I'm happy to use a controller, directive, service, or anything else that works (and is reasonably good practice).
- Disclaimer: I don't understand $compile well.
Here's what I've tried:
// My magic HTML string function.
function htmlString (str) {
return "<h1>" + str + "</h1>";
}
function Ctrl ($scope, $compile) {
$scope.htmlString = htmlString;
}
Ctrl.$inject = ["$scope", "$compile"];
That didn't work.
I tried it as a directive too:
// My magic HTML string function.
function htmlString (str) {
return "<h1>" + str + "</h1>";
}
angular.module("myApp.directives", [])
.directive("htmlString", function () {
return {
restrict: "E",
scope: { content: "@" },
template: "{{ htmlStr(content) }}"
}
});
... and in my HTML ...
<html-string content="foo"></html-string>
Help?
Note
I looked at these, among others, but couldn't make it work.