Skip to content

Instantly share code, notes, and snippets.

@jechav
Last active February 7, 2016 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jechav/7c6c28fc06c86a3882b2 to your computer and use it in GitHub Desktop.
Save jechav/7c6c28fc06c86a3882b2 to your computer and use it in GitHub Desktop.
angular general directives
//********* load image if src dont found **********//
myApp.directive('onErrorSrc', function() {
return {
link: function(scope, element, attrs) {
if(!attrs.src){
attrs.$set('src', attrs.onErrorSrc);
return;
}
element.bind('error', function() {
if (attrs.src != attrs.onErrorSrc) {
attrs.$set('src', attrs.onErrorSrc);
}
});
}
}
});
//Usage
<img ng-src="wrongUrl.png" on-error-src="http://google.com/favicon.ico"/>
////////////////////////////////////////////////////
//********* on mouser hover change class **********//
app.directive('cfClassHover', [function(){
return {
link: function(scope, elem, attrs) {
scope.$watch(attrs.cfClassHover, function(newValue){
elem.bind('mouseover', function(e){
elem.addClass(attrs.cfClassHover);
});
elem.bind('mouseleave', function(e){
elem.removeClass(attrs.cfClassHover);
})
});
}
};
}] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment