2013-10-16 13 views
7

मुझे कोणीय का उपयोग करके मेरी साइट पर एक विनम्र ट्वीट बटन लागू करने में समस्याएं आ रही हैं। मैंने कुछ शोध किया है और jQuery का उपयोग करके AJAX साइट के लिए कुछ समाधान प्राप्त किए हैं, लेकिन जहां तक ​​मैं समाधान (नीचे) का उपयोग करके बता सकता हूं, इसमें नियंत्रक के बाहर चलने वाली स्क्रिप्ट शामिल होगी, और इसलिए गैर-कोणीय और खराब बात होगी।कोणीय तरीके से ट्विटर बटन को कैसे कार्यान्वित करें

क्या किसी ने इस समस्या को कोणीय तरीके से हल किया है या इस पर कोई सुझाव है?

धन्यवाद। AJAX के लिए

समाधान:

//adding button to dom 
$('#tweetbutton').append('<a class="twitter-share-button" href="http://twitter.com/share" data-url="http://www.google.nl>Tweet</a>'); 
//loading widgets 
$.getScript('//platform.twitter.com/widgets.js', function(){ 
//calling method load 
twttr.widgets.load(); 

से लिया: Cannot update tweet button with Ajax

उत्तर

7

यहाँ एक सार है कि आप मदद कर सकता है: https://gist.github.com/Shoen/6350967

directive.js

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 
          } 
         ); 
       }); 
      } 
     } 
    } 
]); 

index.html

<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> 
+0

धन्यवाद मैनुअल। मैं इसे थोड़ी देर के लिए छोड़ने जा रहा हूं, हालांकि जिस विधि को आपने सुझाया है वह आदर्श नहीं है यदि डेटा लोड समय पर लोड नहीं होता है। फिर भी एक अच्छा जवाब हालांकि! – axzr

0

मैं एक ऐसी ही फेसबुक समाधान से एक पृष्ठ पर ले लिया और निम्नलिखित तरीके से इसे लागू करने में सक्षम था।

आपके विचार में, मानक लिंक बटन सिंटैक्स का उपयोग करें:

<a href="https://twitter.com/share" class="twitter-share-button" data-text="Here's something cool">Tweet</a> 
फिर अपने नियंत्रक में

(या यहां तक ​​कि अपने app लोड में) आप पृष्ठ में ठीक से की जरूरत है जावास्क्रिप्ट इंजेक्षन के लिए निम्न कोड का उपयोग करें:

// Load twitter sharing button 
    (function(d,s,id){ 
     var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https'; 
     if(!d.getElementById(id)){ 
      js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js'; 
      fjs.parentNode.insertBefore(js,fjs); 
     } 
    }(document, 'script', 'twitter-wjs')); 

यह पृष्ठ में कोड को ठीक से इंजेक्ट करेगा और आपके ट्विटर बटन को काम करने की अनुमति देगा।

+0

काम नहीं कर रहा है। केवल पहली बार लोड हो रहा है। – marukobotto

संबंधित मुद्दे