Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Last active February 18, 2017 19:59
Show Gist options
  • Save aeinbu/c24cd0cd58b8856d2112029a82b1c220 to your computer and use it in GitHub Desktop.
Save aeinbu/c24cd0cd58b8856d2112029a82b1c220 to your computer and use it in GitHub Desktop.
const angular = require("angular");
const _ = require("lodash");
angular
.module("modulename")
.directive("componentBinder", function($compile, $parse) {
return {
restrict: "E",
scope: {
componentName: "<",
},
link(scope, element, attrs, controller, trancludeFn) {
let transcludeAttrs = _(attrs).omitBy((v, k) => k[0] == "$").omit("componentName").value();
let attrsString = Object.keys(transcludeAttrs).reduce((agg, k) => {
return agg + ` ${_.kebabCase(k)}="${transcludeAttrs[k]}"`
}, "");
let createElement = $compile(`<${scope.componentName} ${attrsString}/>`);
element.append(createElement(scope.$parent));
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment