$sce.trustAsResourceUrl Not Working With Twitter URL on Iframe

时间:2018-06-04 17:01:01

标签: angularjs

I am trying to use $sce.trustAsResourceUrl to sanitize a Twitter URL that I am generating on the controller, however the button that is produced ignores the URL, and uses the current page URL instead.

If I try printing what is in the controller to screen, that works fine.

<iframe id="twitter-widget-0" scrolling="no" frameborder="0" allowtransparency="true" class="twitter-share-button twitter-share-button-rendered twitter-tweet-button" title="Twitter Tweet Button" ng-src="{{ctrl.twitterUniqueURL}}" style="position: static; visibility: visible; width: 60px; height: 20px;"></iframe>

.directive('referral', function ($rootScope, $sce, $interval, regex) {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: $sce.trustAsResourceUrl($rootScope.environment.resources + 'website/html/manage/my-stuff/referral.html'),
        scope: true,
        link: function (scope) {
            scope.ctrl = {
                init: function() {
                    scope.ctrl.getReferralCode();
                },
                form: {
                    email: ''
                },
                twitterUniqueURL: null,
                getReferralCode: function() {
                    var check = $interval(function() {
                        if ($rootScope.ctrl && $rootScope.ctrl.data && 
                        $rootScope.ctrl.data.customer) {
                            scope.ctrl.referralCode = 
                      $rootScope.ctrl.data.customer.referralCodeForCustomer;
            scope.ctrl.twitterUniqueURL = $sce.trustAsResourceUrl("https://platform.twitter.com/widgets/tweet_button.b813cd3227574096a07e094b73331535.en.html#dnt=false&amp;id=twitter-widget-0&amp;lang=en&amp;original_referer=https%3A%2F%2Fexample.co.uk%2Faccount%2F%3Freferral%26Code%3D" + scope.ctrl.referralCode + "&amp;size=m&amp;text=Receive%20%C2%A310%20when%20signing%20up%20for%20example.%20Live%20a%20Bigger%20Life%2C%20One%20Box%20at%20a%20Time.&amp;time=1528123666174&amp;type=share&amp;url=https%3A%2F%2Fexample.co.uk%2F%3FReferral%3D" + scope.ctrl.referralCode + "&amp;via=exampleUK");
                        }
                    }, 300)                         
                },
            };
        scope.ctrl.init();
        }
    };
})

Any ideas?

Apologies if I haven't explained this very well - only started using AngularJS two weeks ago.

Thanks James

0 个答案:

没有答案