Created
August 29, 2017 18:28
-
-
Save anteriovieira/631227e0857723c744bf019483854605 to your computer and use it in GitHub Desktop.
Ng-loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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