Skip to content

Instantly share code, notes, and snippets.

@ryanemmm
Last active February 27, 2018 02:05
Show Gist options
  • Save ryanemmm/9087002 to your computer and use it in GitHub Desktop.
Save ryanemmm/9087002 to your computer and use it in GitHub Desktop.
Implement coinbase button (html + js tag) in Angularjs view
# Used a directive to inject a 'hook' element
# + javascript tag that consumes/acts on said
# 'hook' via an anon, self-exececuting fn that
# fires when the script loads.
#
# The problem this solves is a race condition
# where the javascript executes prematurely
# and throws a SAMEORIGIN error.
<!-- html/directive call -->
<coinbase-button-here data-code="button-code" />
<!-- angular -->
angular.module('DirectiveParent', [])
.directive('coinbase-button-here', function () {
return {
restrict: 'E',
link: function (scope, element, attrs) {
//$watch the attrs.code, handle undefined
scope.$watch(attrs.code, function(code, oldcodevalue){
//bail if code is undefined
if(code === undefined){return;}
//inject the button and script tag
angular.element('<div class="coinbase-button" data-code="' + code +"></div>').appendTo(element);
angular.element('<script src="https://coinbase.com/assets/button.js"></script>').appendTo(element);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment