Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS directive that uses the original element type in template
    primarykey
    data
    text
    <p>I'm developing UI and typography based directives for Angular. In such cases, the element the directive is being applied on is unknown - anything from a div, span, h1 to an h5.</p> <p>The reason for using a template is so I can add <code>ng-*</code> directives to it (so the developer doesn't need to remember anything but the directive name).</p> <p>I've had limited success adding attributes and recompiling the element. No success when it comes to adding <code>ng-transclude</code> however. Creating a new element and replacing the old comes with integration issues (ignore other directives and data attributes that may be on the element), had little success copying these attributes and adding them to the new element.</p> <p>This seems like it should be really simple, as <code>template</code> itself can change the element to whatever you specify (using <code>transclude</code> and <code>replace</code>), surely there's the 'long way of doing it'?</p> <p>It's too bad you can't do the following:</p> <pre><code>module.directive( 'myDir', [ '$window', function( $window ) { return { restrict: "A", controller: function( $scope, $element ) { $scope.tag = $element[0].nodeName; } template: "&lt;{{tag}} data-ng-transclude ng-*=''&gt;&lt;/{{tag}}&gt;", transclude: true, replace: true, link: function ( scope, element, attrs ) { } } }]); </code></pre> <p>I think the major issue is I'm looking to replace and transclude the element with the template, not add the template (or compile the element) as a child.</p> <p>I have since replaced the need for <code>ng-*</code> and templates with vanilla JS in my code, eg:</p> <pre><code>&lt;div data-ng-style="{'font-size':fontSize}&gt;&lt;/div&gt; </code></pre> <p>with</p> <pre><code>element[0].style.fontSize = scope.fontSize; </code></pre> <p>Which begs the question of the benefit of using many <code>ng-*</code> directives? Is it just 'the Angular way'?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload