Skip to content

Instantly share code, notes, and snippets.

@anteriovieira
Created August 29, 2017 18:28
Show Gist options
  • Save anteriovieira/631227e0857723c744bf019483854605 to your computer and use it in GitHub Desktop.
Save anteriovieira/631227e0857723c744bf019483854605 to your computer and use it in GitHub Desktop.
Ng-loader
<template>
<div></div>
</template>
<script>
import {kebabCase} from 'lodash';
export default {
bindings: {type: '<'},
controller: class ComponentCtrl {
static $inject = ['$scope', '$element', '$compile', '$attrs'];
constructor($scope, $element, $compile, $attrs) {
$scope.$watch('$ctrl.type', component => {
component = kebabCase(component);
$element.html(`<${component} ${this.normalizeAttrs($attrs)}></${component}>`);
$compile($element.contents())($scope.$parent);
});
}
normalizeAttrs(attrs) {
let attributes = [];
Object.keys(attrs).forEach(name => {
if (name.charAt(0) !== '$') {
attributes.push(`${name}='${attrs[name]}'`);
}
});
return attributes.join(' ');
}
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment