0

Is it possible to hide my directive html element? I mean if I have this directive:

app.js

angular.module("myApp", [])
    .directive("myDirective", function() {
        return {
            restrict: "E",
            template: "<div><p>Some text...</p></div>"
    }
);

Only render the content of the template.

<div>
    <p>Some text...</p>
</div>

Not the directives element:

<my-directive>
    <div>
        <p>Some text...</p>
    </div>
</my-directive>
juanmorschrott
  • 573
  • 5
  • 25

2 Answers2

2

yes just put the replace: true in the directive. it will remove the element

angular.module("myApp", [])
    .directive("myDirective", function() {
        return {
            restrict: "E",
            template: "<div><p>Some text...</p></div>",
            replace: true
    }
);

Demo

angular.module("app",[])
.controller("ctrl",function($scope){


}).directive("myDirective", function() {
        return {
            restrict: "E",
            template: "<div><p>Some text...</p></div>",
            replace : true
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <my-directive> 
</my-directive>
</div>
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
1

Note that replace: true is marked as deprecated (I use it myself nonetheless from time to time)

See: Explain replace=true in Angular Directives (Deprecated)

flyer
  • 1,414
  • 12
  • 13