1

I have a function, renderFOO(value), that returns a DOM element. How can I have that element rendered inside an AngularJS application, in function of a variable in a model?

I've managed to solve it using a directive:

<input type="range" ng-model="foo">
<foo></foo>

app.directive("foo", function(){
    return {
        restrict: "E",
        link : function(scope, elem, attr){
            scope.$watch("foo",function(periodo){
                elem[0].innerHTML = "";
                elem[0].appendChild(renderFOO(foo));
            });
        }}
});

But I think that is big and probably not idiomatic and the solution would be something like:

<input type="range" ng-model="foo">
<div>{{renderFOO(foo)}}</div>

Which does not work.

7
  • Where is the renderFOO function? I mean is it scoped? Is it from another library? Commented Nov 6, 2015 at 17:12
  • what are you trying to do? mostley its not a very good idea to manipulate the DOM directly when using AngularJS Commented Nov 6, 2015 at 17:13
  • I'm rendering an SVG chart dynamically, so I created the renderFOO function which returns the chart as I need it. Commented Nov 6, 2015 at 17:24
  • @Viclib were you able to make any progress on this? Was my suggestion able to help you? Commented Nov 9, 2015 at 15:18
  • @scniro I've ran out of time for this issue for now, so I left it in a non optimal way. I will come back to this problem eventually, test your suggestion and give you a feedback. Thank you! Commented Nov 9, 2015 at 16:17

1 Answer 1

1

If I understand correctly, you should be able to leverage ngBindHtml in the ngSanitize module - offering the ability to bind DOM elements to some value. Observe the following simplified example...

<button ng-click="renderFoo()">render</button>
<div ng-bind-html="foo"></div>

$scope.renderFoo = function() {
    $scope.foo = '<span class="example">stuff</span>'
}

JSFiddle Link - simplified demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.