1

I'd like to create an example directive that appends its inner HTML to itself. So, this:

<div example>
  <label for="name">Name:</label>
  <input id="name" type="text" ng-model="name">
</div>

should become this:

enter image description here

When inside a directive, the element already has things line class="ng-scope ng-pristine ng-valid" which shouldn't be in the outputted HTML.

How would implement such a directive?

My attempt is here

1
  • you would need html from $compile, but not sure how to print it... given that $scope is not available during compile time Commented Oct 6, 2014 at 11:16

1 Answer 1

2

You probably don't need to transclude or use scope here, just use the compile function to grab the inner html and append it to the node:

.directive('example', function() {
  return {
    compile: function(ele) {
      var innerHtml = ele.html();
      ele.append(document.createTextNode(innerHtml));
    }
  };
});

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.