Skip to content

Instantly share code, notes, and snippets.

@rcline
Last active June 5, 2020 20:47
Show Gist options
  • Save rcline/0112c3ec6fd2f84c986c9ff0a6c5d5c0 to your computer and use it in GitHub Desktop.
Save rcline/0112c3ec6fd2f84c986c9ff0a6c5d5c0 to your computer and use it in GitHub Desktop.
/**
* Useage:
* <svelte component="MySvelteComponent" all-props="allPropsFromAngularJS"></svelte>
*
*/
(function() {
'use strict';
angular
.module('myapp')
.directive('svelte', svelteDirective);
function svelteDirective() {
return {
restrict: "E",
scope: {
component: '=',
allProps: '=',
},
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) {
var Component;
var attrKeys = Object.keys(iAttrs.$attr);
if (attrKeys.length > 0) {
attrKeys.forEach(function (key) {
if (iAttrs.$attr[key] === 'component') {
Component = scope[key];
}
});
}
if (!Component) {
throw Error('You must supply a component to svelte directive.');
}
var component = new Component({
target: iElement[0],
props: scope.allProps,
});
// Attempting to get binding working:
console.log('************************', scope.allProps.noteCount, component);
scope.$watch('allProps', function (newVal, oldVal) {
console.log('************************',newVal, oldVal, scope.allProps.noteCount, component);
component.set(newVal);
}, true);
/*
scope.$watch("allProps.noteCount", function (newVal, oldVal) {
console.log('************************',newVal, oldVal, scope.allProps.noteCount);
});*/
}
};
}
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment