Skip to content

Instantly share code, notes, and snippets.

@dpogorzelski
Created August 27, 2013 08:16
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dpogorzelski/6350967 to your computer and use it in GitHub Desktop.
Save dpogorzelski/6350967 to your computer and use it in GitHub Desktop.
Twitter share button as AngularJS directive.
directives.directive('twitter', [
function() {
return {
link: function(scope, element, attr) {
setTimeout(function() {
twttr.widgets.createShareButton(
attr.url,
element[0],
function(el) {}, {
count: 'none',
text: attr.text
}
);
});
}
}
}
]);
<script src="http://platform.twitter.com/widgets.js"></script>
<a twitter data-text="Your text: {{file.name}}" data-url="http://127.0.0.1:3000/{{file.link}}"></a>
@johnstew
Copy link

johnstew commented Nov 7, 2013

What is the point of the Timeout? Just curious.

@sean-hill
Copy link

Probably to wait for attr.text to be loaded by angular so it's not something like {{tweetText}}

@Soviut
Copy link

Soviut commented Feb 27, 2014

This should probably use $timeout instead of setTimeout so that it waits for the next digest cycle rather than the next browser frame.

@buffala
Copy link

buffala commented Apr 12, 2014

How can we use a custom image?

@calebjonasson
Copy link

@buffala You can send an image url in the tweet string or make sure that the article you are linking to contains the appropriate twitter meta data for the image to be picked up. Other than that you are out of luck unless you want to use the API.

@ignacio-chiazzo
Copy link

How can I change the style of the button?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment