Chapter 1: Working through the directive spectrum - The Element Directive

by billy roberts

HTML

<script src="https://code.angularjs.org/1.3.2/angular.min.js"></script>
<div ng-app="myApp">
    <element-directive some-attr="myvalue">
        <!-- directive's HTML contents -->
    </element-directive>
</div>

JavaScript

// use of replace: true
// must inspect element to see the difference
// in the compiled output.
angular.module('myApp', [])
.directive('elementDirective', function ($log) {
    return {
        restrict: 'E',
        replace: true,
        template: '<p>Ze template!</p>',
        link: function(scope, el, attrs) {
            $log.log(el.html());
            // Ze template!
            $log.log(attrs.someAttr);
            // myvalue
        }
    };
});