oikonomiyaki
oikonomiyaki

Reputation: 7951

Text between directive tags

Suppose I have a directive named my-directive.

How do I access or manipulate the text between the directive tags in the directive code, like this:

<my-directive> Custom Text <my-directive>

And my directive code:

app.directive('myDirective', function() {
    return {
        ...
        template: '<div>...Custom Text somewhere inside div... </div>'
    };
); 

Upvotes: 3

Views: 4867

Answers (2)

Enzey
Enzey

Reputation: 5254

Use the ng-transclude directive in your template.

.directive('myDirective', function() {
    return {
        transclude: true,
        template: '<div><ng-transclude></ng-transclude></div>
    }
});

Moves the previous inner content to where the ng-transclude is in the template.

Demo in Plunker

Upvotes: 15

simon
simon

Reputation: 1485

if you give template in your directive. it will replace the text inside the html

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    compile: function(elem) {
      elem.replaceWith(markdown.toHTML(elem.html()));
    }
  }
});

Upvotes: 0

Related Questions