Skip to content

Instantly share code, notes, and snippets.

@danharper
Created March 6, 2015 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danharper/6d7bf19b6c081ff7cc0a to your computer and use it in GitHub Desktop.
Save danharper/6d7bf19b6c081ff7cc0a to your computer and use it in GitHub Desktop.
ngDirective - note I haven't exactly dove deep into directives, so I'm sure there are other forms this doesn't account for (e.g. only having the link method?)
import ngInject from './ngInject'
export default function ngDirective(directive) {
let func = function(...injectedArgs) {
let link = (...directiveArgs) => new directive(...injectedArgs, ...directiveArgs)
return {...directive.ddo(), link}
}
func.$inject = ngInject(directive).$inject
return func
}
class InspectionCardDirective {
static ngInject() {
return ['fooService']
}
static ddo() {
return {
retrict: 'E',
templateUrl: 'some/path.html',
scope: {
inspection: '=inspection'
}
}
}
constructor(fooService, scope, element, attrs) {
// my args are:
// injected args from ngInject
// followed by args for the link function (scope, element, attrs, ...etc)
}
}
// then when registering:
.directive('inspectionCard', ngDirective(InspectionCardDirective))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment