Skip to content

Instantly share code, notes, and snippets.

@inkless
Created July 19, 2016 20:26
Show Gist options
  • Save inkless/6f8d9607952245d621182aa0e8e2d05e to your computer and use it in GitHub Desktop.
Save inkless/6f8d9607952245d621182aa0e8e2d05e to your computer and use it in GitHub Desktop.
Override previous directives or filters in angular
/**
* unregister
*
* unregister old directives or filters
*
* @param {Object.<{name: string, type: string}>[]} declares
* @param string module
* @returns {undefined}
* @example
* {name: 'foo', type: 'directive')
* type can only be `directive` or `filter`
* Services and factories will be override by angular itself
*/
function unregister(declares, module) {
if (!angular.isArray(declares)) {
declares = [declares];
}
declares.forEach(function(declare) {
if (angular.isString(declare)) {
declare = { name: declare };
}
var type = '';
switch(declare.type) {
case 'directive':
type = 'Directive';
break;
case 'filter':
type = 'Filter';
break;
default:
type = 'Directive';
break;
}
angular.module(module).config(['$provide', function($provide) {
$provide.decorator(declare.name + type, ['$delegate', function($delegate) {
if ($delegate.length > 1) {
$delegate.shift();
}
return $delegate;
}]);
}]);
});
}
// sample usage:
// by default it's directive
unregister('foo', 'Foo');
// or
unregister({name: 'foo'}, 'Foo');
// or
unregister([{name: 'foo'}, {name: 'bar', type: 'filter'}], 'Foo');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment