Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gaelbillon/5661596 to your computer and use it in GitHub Desktop.
Save gaelbillon/5661596 to your computer and use it in GitHub Desktop.
could be useful in case of eg: websockets pushing data that can randomly be defined or undefined, if the new value coming in is undefined, this directive keep the old value displayed.
angular.module('myApp.directives', []).
directive('keepoldvalueifnewisempty', function() {
return {
restrict: "E",
link: function(scope, elm, attrs) {
scope.$watch(attrs.val, function (newValue, oldValue) {
newValue = !newValue ? oldValue : newValue;
elm.html(newValue);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment