Skip to content

Instantly share code, notes, and snippets.

@carlosrymer
Last active September 2, 2016 02:24
Show Gist options
  • Save carlosrymer/9883733 to your computer and use it in GitHub Desktop.
Save carlosrymer/9883733 to your computer and use it in GitHub Desktop.
A custom directive that enhances ngStyle by observing style changes so that if a variable in an expression changes, it can update the style as well. This is useful when binding to a resource promise in styles.
app.directive("myStyle", function (){
return {
restrict: 'A',
link: function(scope, element, attrs)
{
var el = element[0],
attr = el.getAttribute('style');
el.setAttribute('style', attr);
// We need to watch for changes in the style in case required data is not yet ready when compiling
attrs.$observe('style', function (){
attr = el.getAttribute('style');
if(attr)
{
el.setAttribute('style', attr);
}
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment