简体   繁体   English

AngularJS-Facebook分享者

[英]AngularJS - Facebook Sharer

I am trying to use the Facebook Share in AngularJS. 我正在尝试在AngularJS中使用Facebook Share。 Below is my function that is called when the user clicks on the FB icon. 下面是用户单击FB图标时调用的我的函数。

    $scope.shareFB = function(){

        // Get configuration ID from service
        configuratorService.storeConfiguration($scope.modelCode, function(configID){
            // Use saved configuration id to create share link
            var base = $location.absUrl().replace($location.url(), '');
            var byoUrl = base + "/" + $scope.modelCode + "/resume/" + configID;
            console.log(byoUrl);
            var fbpopup = window.open("https://www.facebook.com/sharer/sharer.php?u=" + byoUrl, "pop", "width=600, height=400, scrollbars=no");
        });

    }

This function works fine when I try to share a url like " https://www.google.com/ " 当我尝试共享“ https://www.google.com/ ”之类的网址时,此功能可以正常工作

the Facebook Popup then has the URL = " https://www.facebook.com/sharer/sharer.php?u=https://www.google.com/ " 然后,Facebook Popup的URL =“ https://www.facebook.com/sharer/sharer.php?u=https://www.google.com/

When I use the function above: 当我使用上面的函数时:

byoUrl = " http://localhost:8000/#/15K6/resume/9295316837 " byoUrl =“ http:// localhost:8000 /#/ 15K6 / resume / 9295316837

and the resulting FB popup has URL = " https://www.facebook.com/15K6/resume/9295316837 " 并且结果FB弹出窗口具有URL =“ https://www.facebook.com/15K6/resume/9295316837

Why does the "/sharer/sharer.php?= http://localhost:8000/#/ " get cut off? 为什么“ /sharer/sharer.php?= http:// localhost:8000 /#/ ”被切断?

You shouldn't even try to share a localhost URL, as Facebook will never be able to scrape it. 您甚至不应该尝试共享本地主机URL,因为Facebook将永远无法抓取它。 That's very likely why your URL gets cut off. 这很可能是您的URL被切断的原因。 Facebook tries to resolve it and scrape it, but it will never find it, so it makes a best effort to redirect within itself. Facebook尝试解决并抓取它,但它永远找不到,因此它会尽最大努力在自身内进行重定向。 Example: 例:

https://www.facebook.com/sharer/sharer.php?u=http://localhost:8000/#/coke https://www.facebook.com/sharer/sharer.php?u=http://localhost:8000/#/coke

Try to put your share logic in the controller. 尝试将共享逻辑放入控制器中。 Something along these lines. 遵循这些原则。

  // Share posts $scope.fbShare = function(post){ FB.ui( { method: 'feed', name: post.title, link: 'http://www.cengkuru.com/'+post.slug, picture: '', caption: '', description: $filter('limitTo')($scope.post.body, 150), message: '' }); } 
 <div id="fb-root"></div> <script> window.fbAsyncInit = function() { FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true, xfbml: true}); }; (function() { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); }()); </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM