I am looking for the best practice for angular 1 component and CMS driven approach.
I am planning to build multiple label templates and I want this project to be component driven, highly reusable and driven by a CMS content.
I am planning to use JSON as a tree of components and just compile the tree step by step using $compile service like this:
angular.module('app.compile', [], function($compileProvider) {
$compileProvider.directive('compile', function($compile) {
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
}
);
};
});
});
http://plnkr.co/edit/MwUjE9l6U5wMkE89kwqY?p=preview
$compile
service might be bad for performance?I prefer to use angular components. I use $compile just for dynamic usage of my components. Use directives just only to change the DOM. If your control has a template use component instead.
Check that sample angular components with typescript
Tks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With