Skip to content

Instantly share code, notes, and snippets.

@chrisfey
Last active August 29, 2015 14:07
Show Gist options
  • Save chrisfey/dc069e51fe8b97687ad9 to your computer and use it in GitHub Desktop.
Save chrisfey/dc069e51fe8b97687ad9 to your computer and use it in GitHub Desktop.
reloading twitter widget directive for angular js
angular.module('ng-twitter')
.directive('twitter', function() {
return {
template: '<div><div class="ng-twt-widget-preview"><!-- twitter preview will go in this element --></div></div>',
restrict: 'A',
controller: 'TwitterController',
scope: {
widgetId: '='
},
link: function(scope, element, attrs) {
scope.element = element;
}
};
}).controller('TwitterController', ['$scope', function($scope) {
window.twttr = (function(d, s, id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id))
return;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = {_e: [], ready: function(f) {
t._e.push(f)
}});
}(document, "script", "twitter-wjs"));
$scope.$watch('widgetId', function() {
$scope.element.find('.ng-twt-widget-preview').remove();
$scope.element.find('iframe').remove();
$scope.element.append('<a class="twitter-timeline ng-twt-widget-preview" href="#" data-widget-id="' + $scope.widgetId + '"></a>');
twttr.widgets.load();
});
}]);
@BorjaL
Copy link

BorjaL commented Apr 16, 2015

Thanks mate! Your code saved me a lot of time!!!

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