BenNadel_2712

http://www.bennadel.com/blog/2712-refactoring-bnrepeatswitch-to-use-a-multi-priority-directive-in-angularjs.htm

by ExpertSystem

HTML

<script src="http://code.angularjs.org/1.3.2/angular.min.js"></script>
<div dir-a="" dir-b="" dir-c=""></div>

<small>Nothing to see here ?</small><br />
<b>Open the console !</b>

<!-- -=-=-=-=- -=[IGNORE BELOW THIS LINE]=- -=-=-=-=- -->
<!-- Page Footer -->
<small class="footer" ng-controller="footerCtrl">
    <b>Angular v{{version.full}}</b> ({{version.codeName}})
    <span class="right">
        &copy;
        <a ng-href="{{author.url}}" target="_blank">{{author.name}}</a>
    </span>
</small>

CSS

/* -=-=-=-=- -=[IGNORE BELOW THIS LINE]=- -=-=-=-=- */
/* Page Footer */
body { padding-bottom: 22px; }
.footer { background-color: White; border-top: 1px solid rgba(100, 100, 100, 0.33); bottom: 0; display: block; font-size: 0.75em; height: 20px; left: 0; margin: 0 5px; overflow: auto; padding: 5px; position: fixed; right: 0; }
.right { float: right; }

JavaScript

var app = angular.module('myApp', []);

app.directive('dirA', dirGenerator('dirA', 1001, false));
app.directive('dirB', dirGenerator('dirB', 1000, true));
app.directive('dirC', dirGenerator('dirC', 999, false));
              
function dirGenerator(name, priority, transclude) {
    return function () {
        return {
            priority: priority || 0,
            transclude: !!transclude,
            compile: function () {
                console.log('Compiling ' + name + '...');
                return {
                    pre: function () {
                        console.log('Pre-linking ' + name + '...');
                    },
                    post: function () {
                        console.log('Post-linking ' + name + '...');
                    }
                };
            },
            controller: function () {
                console.log('Controller for ' + name + '...');
            }
        };
    };
}

/* -=-=-=-=- -=[IGNORE BELOW THIS LINE]=- -=-=-=-=- */
/* Page Footer */
app.controller('footerCtrl', function ($scope) {
    $scope.version = angular.version;
    $scope.author  = {
        name: 'ExpertSystem',
        url:  '//stackoverflow.com/users/2341938/expertsystem'
    };
});