Created
January 26, 2015 23:03
-
-
Save ameyms/4f3a5d6a8d329609d44e to your computer and use it in GitHub Desktop.
A directive and provider pair to apply arbitrary JQuery plugins to elements
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
angular.module( 'ameyms.jqplugin' ). | |
directive( 'applyPlugin', [ 'jqueryPlugins', function ( jqPlugins ) { | |
var postLinkFn; | |
postLinkFn = function ( scope, elem, attrs ) { | |
var currPlugin = attrs.applyPlugin, | |
opts = jqueryPlugins.getOptionsFor( currPlugin ); | |
// Apply plugin | |
$( elem )[ currPlugin ]( opts ); | |
}; | |
return { | |
restrict: 'A', | |
link: postLinkFn | |
}; | |
} ] | |
); |
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
angular.module( 'ameyms.jqplugin' ). | |
provider( 'jqueryPlugins', function () { | |
var pluginOptions = {}; | |
this.$get = function () { | |
return { | |
getOptionsFor: function ( pluginName ) { | |
return pluginOptions[ pluginName ]; | |
}, | |
setOptions: function ( pluginName, opts ) { | |
pluginOptions[ pluginName ] = opts; | |
return this; | |
} | |
}; | |
}; | |
} ); |
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
angular.module( 'ameyms.jqplugin', [] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
In your Javascript:
And in your templates: