1

i have this element in my html:

<div class='printOptionElements'></div>

and i want fill it dynamicly with ng-click-elements in my Controller.

$scope.printoptions = function() {
         $(".printOptionElements").append("<div class='printoption printhideelement' ng-click='printhide()'>Hide</div>");
    }
if (document.URL.indexOf("print=true") > 0) {
         $scope.printoptions();
}

$scope.printhide = function() {
        alert("hidetest");
    }

The printoption element will appear, but the ng-click will not work. I think that angular shows for ng-clicks before this ng-click is there. What can i do to "activate" the ng-click?

Thanks for help :)

Greetings Thomas

1 Answer 1

3

You need to inject the $compile service into your controller and then compile the content against the controller's scope before appending it:

$scope.printoptions = function() {
        var content = "<div class='printoption printhideelement' ng-click='printhide()'>Hide</div>";

        $(".printOptionElements").append($compile(content)($scope));
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Unless you are seeing any more issues relating to this code, can you mark the answer as accepted?
Indeed. Just needed to wait a few minutes before i could to it.

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.