Last active
September 2, 2016 02:24
-
-
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.
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
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