Skip to content

Instantly share code, notes, and snippets.

@bugspencer
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save bugspencer/d78f9d56b44adbb62843 to your computer and use it in GitHub Desktop.

Select an option

Save bugspencer/d78f9d56b44adbb62843 to your computer and use it in GitHub Desktop.
ng-directive (compact)
px('moduleName', function(){
/**
* directive definition of `###directiveName###`
*
*/
this.directive('###directiveName###', ###directiveName###Factory);
###directiveName###Factory.$inject = [];
function ###directiveName###Factory(){
var directiveDefinitionObject = {
// priority: 0,
// terminal: false,
// replace: true,
// scope: false,
// template : '<h1>Dashboard</h1>',
// templateUrl: '',
// transclude: false,
//
// controllerAs: '###directiveName###',
// controller: '###directiveName###DirectiveController',
//
// require: '###directiveName###',
// compile : compileFn,
// link : postLinkFn,
restrict: 'AC'
};
return directiveDefinitionObject;
/*=== FUNCTIONS ===*/
/**
* link function
*
* it will be executed after the scope is created
* ---
* normally use for registering DOM listeners (i.e., $watch expressions on the scope) as
* well as updating the DOM (i.e., manipulation of iElement = individual instance element).
* It is executed after the template has been cloned -- e.g., inside an <li ng-repeat...>,
* the link function is executed after the <li> template (tElement) has been cloned (into
* an iElement) for that particular <li> element. A $watch allows a directive to be
* notified of scope property changes (a scope is associated with each instance), which
* allows the directive to render an updated instance value to the DOM.
*
* @param {Object} scope scope Object which is used to to compile the iElement
* @param {Element} iElement element object which is used for this specific directive
* @param {Object} iAttrs attribute list of the directive given by the DOM
* @param {Object} controller if there is a controller list or single controller
* require the controller instance will be passed
* in the Object
* @param {Function} transcludeFn this function makes it possible to take the innerHtml
* of the directive and us it
* `transcludeFn ([(new)scope], cloneLinkingFn, futureParentElement)`
* @return {void}
*/
function postLinkFn(scope, iElement, iAttrs, controller, transcludeFn) {
controller.activate();
}
/**
* compile Function
*
* it will be executed before the template is compiled with the scope
* ---
* use for template DOM manipulation (i.e., manipulation of tElement = template element),
* hence manipulations that apply to all DOM clones of the template associated with the
* directive. (If you also need a link function (or pre and post link functions), and you
* defined a compile function, the compile function must return the link function(s)
* because the 'link' attribute is ignored if the 'compile' attribute is defined.)
*
*
* @param {Element} tElement template element
* @param {Object} tAttrs attribute list of the directive given by the DOM
* @return {Function|Object} Link function or
* Object { pre: function... , post: function }
*/
function compileFn (tElement, tAttrs){
return postLinkFn;
}
}
/**
* controller definition for the `###directiveName###` directive
*
* must be used when another directive needs to interact with this directive. E.g., on the
* AngularJS home page, the pane directive needs to add itself to the scope maintained by the
* tabs directive, hence the tabs directive needs to define a controller method (think API) that
* the pane directive can access/call.
*
*/
this.controller('###directiveName###DirectiveController', ###directiveName###DirectiveControllerFactory);
###directiveName###DirectiveControllerFactory.$inject = ['$scope','$element','$attrs','$transclude'];
function ###directiveName###DirectiveControllerFactory ($scope, $element, $attrs, $transclude) {
/**
* ViewModel
* @type {###directiveName###DirectiveControllerFactory}
*/
var vm = this;
vm.activate = activate;
/*=== Functions ===*/
function activate () {
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment