Last active
February 7, 2016 01:38
-
-
Save jechav/7c6c28fc06c86a3882b2 to your computer and use it in GitHub Desktop.
angular general directives
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
//********* 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