Skip to content

Instantly share code, notes, and snippets.

@aclave1
Last active August 29, 2015 14:17
Show Gist options
  • Save aclave1/fd5e31bf5bd5e2a4370f to your computer and use it in GitHub Desktop.
Save aclave1/fd5e31bf5bd5e2a4370f to your computer and use it in GitHub Desktop.
A provider for a "directive builder". Inject this provider into a directive declaration and return its invocation and it will build a directive definition object. I got this idea from angular-ui-bootstrap's code here: https://github.com/angular-ui/bootstrap/blob/master/src/tooltip/tooltip.js#L12
module.exports = function () {
this.$get = [function () {
return function directiveBuilder(config) {
var options = config || {};
return {
replace: true,
restrict: "E",
template: "<div></div>",
//template:function(telem,tattrs){return "<div></div>";}
//templateUrl:'/url/to/template',
//transclude:true,false,element
//scope:true,false,{}
//controllerAs
//require:'othercontroller' | ['multiple','controllers'],'^parent'
controller: ["$scope", function ($scope) {
}],
compile:function(tElem, attrs){
return {
post:function postLink(scope, iElement, iAttrs, controller){},
pre:function preLink(scope, iElement, iAttrs, controller){
/**
* useful if you want to operate on the scope BEFORE your children compile.
* If this directive has child directives inside of it
* and it wants to set a property on the scope for them to see, it needs to do that in prelink.
* */
}
};
}
};
};
}];
};
var directiveBuilder = require('./directivebuilder');
angular.module('gistmodule',[])
.provider('directivebuilder',directiveBuilder)
.directive('myDirective',['directivebuilder',function(directivebuilder){
return directivebuilder({option1:'',option2:''});
}])
.directive('myOtherSimilarDirective',['directivebuilder',function(directivebuilder){
return directivebuilder({option1:'',option2:''});
}])
;
//template
<my-directive></my-directive>
<my-other-similar-directive></my-other-similar-directive>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment