Skip to content

Instantly share code, notes, and snippets.

@elgervb
Last active April 6, 2016 14:10
Show Gist options
  • Save elgervb/e2d0bc9d38f527cd4af34814de6b4c0a to your computer and use it in GitHub Desktop.
Save elgervb/e2d0bc9d38f527cd4af34814de6b4c0a to your computer and use it in GitHub Desktop.
TypeScript angular 1.5 directive
/**
* Angular Directive to collapse a panel
*
* Params:
* name {string} the name of the directive (bound by angular.module.directive(name, fn))
*
* Use:
* <div class="sidebar">
* <div class="toggle" collapse=".sidebar"> </div>
* </div>
*
* Clicking on the toggle will collapse the sidebar
*
* Register:
* angular.module(<modname>).directive('{name}', PanelCollapseDirective.factory({name}))`
*/
class PanelCollapseDirective implements ng.IDirective {
public name: string;
public restrict: string = "A";
public scope: Object = {};
public link: ng.IDirectiveLinkFn = (scope: ng.IScope, el: ng.IAugmentedJQuery, attr: ng.IAttributes) => {
el.bind('click', () => {
document.querySelector(attr[this.name]).classList.toggle('collapsed');
});
};
constructor() {}
static factory(name: string): ng.IDirectiveFactory {
let factory: ng.IDirectiveFactory = () => {
let directive = new PanelCollapseDirective();
directive.name = name;
return directive;
}
// dependency injection goes here. Params are received through directive constructor
// factory.$inject = ['dep1', 'dep2'];
return factory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment