6

I have an update button with a directive. When the button is clicked, the targeted element should receive some new html which includes a ngInclude element. It does not seem to be loading the file though, all it does is include a comment like this <!-- ngInclude: nav.html -->. If I log the tpl variable I get { 0: <!-- ngInclude: nav.html -->, length: 1 }. Here is my directive and element generator code.

Directive

angular.module('myApp').directive("contentControl"
,["$compile"
,function($compile){
    return {
        link: function(scope, element, attrs) {
            element.bind("click", function () {
                var $container = $(this).closest('#editor_contenteditorcontainer');
                var html = "";
                $container.find('.row').each(function () {
                    var $args = $(this).find('form').serializeObject();
                    html += ' ' + generateContent($args);
                });
                angular.element(document.getElementById('responsiveviewport')).html(html);
                $compile(html)(scope);
                scope.$apply();
            });
        }
    }
}]);

Generator

function generateContent($arg){
  switch($arg['name']){
    case 'Partial':
        return '<div ng-include src="\''+$arg['content']+'\'"></div>';
        break;
    default:
        break;
  }
}
2
  • you need to do $scope.$apply() from click event to run digest cycle Commented Apr 4, 2015 at 6:00
  • 1
    @pankajparkar thanks for the response. I have updated my answer. I found that the $compile needed to be after adding it to the element. I have added scope.$apply() also. It will now load the files but it still does not apply the contents of those files into the ng-include Commented Apr 4, 2015 at 6:50

2 Answers 2

6

You would need to compile the place where you inserted generated content.

.directive('contentControl', ['$compile' ,function($compile){
    return {
      template: 'Click here',
      link: function(scope, element, attrs) {
          element.bind('click', function () {
              var html = "<p>Template:</p><div ng-include src=\"'template.html'\"></div>";

              var templateGoesHere = angular.element(document.getElementById('templateGoesHere'));
              templateGoesHere.html(html);

              $compile(templateGoesHere)(scope);

              scope.$apply();
          });
      }
    }
}]);

See Plunker

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

1 Comment

My problem was that i forgot to call scope.$apply() in my solution. Thank you
1

$compile(element)(scope) returns an element that you should place in the DOM. The .html() part of your code is just inserting your uncompiled html, and you're not doing anything with the compiled element. Instead, you could do something like:

angular.element(el).empty().append($compile(html)(scope))

1 Comment

I was not able to get this to work doing it this way. Compiling the element instead of the html worked.

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.