-
-
Save jcaxmacher/147ee10819b3f9c7629a to your computer and use it in GitHub Desktop.
This file contains 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
angular.module('kcd.directives').directive('kcdRecompile', function($compile, $parse) { | |
'use strict'; | |
return { | |
scope: true, // required to be able to clear watchers safely | |
compile: function(el) { | |
var template = el.html(); | |
return function link(scope, $el, attrs) { | |
scope.$parent.$watch(attrs.kcdRecompile, function(_new, _old) { | |
var useBoolean = attrs.hasOwnProperty('useBoolean'); | |
if ((useBoolean && (!_new || _new === 'false')) || (!useBoolean && (!_new || _new === _old))) { | |
return; | |
} | |
// remove all watchers because the recompiled version will set them up again. | |
removeChildrenWatchers($el); | |
// reset kcdRecompile to false if we're using a boolean | |
if (useBoolean) { | |
$parse(attrs.kcdRecompile).assign(scope.$parent, false); | |
} | |
// recompile | |
var newEl = $compile(template)(scope.$parent.$new()); | |
$el.html(newEl); | |
}); | |
}; | |
} | |
}; | |
function removeChildrenWatchers(element) { | |
angular.forEach(element.children(), function(childElement) { | |
removeAllWatchers(angular.element(childElement)); | |
}); | |
} | |
function removeAllWatchers(element) { | |
if (element.data().hasOwnProperty('$scope')) { | |
element.data().$scope.$$watchers = []; | |
} | |
removeChildrenWatchers(element); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment