AngularJS - 异步调用API的Facebook [英] AngularJS - Asynchronous call to Facebook API

查看:133
本文介绍了AngularJS - 异步调用API的Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我'尝试调用与AngularJS一个Facebook的API。

I'am trying to call a Facebook API with AngularJS.

问题是,从FB API的所有调用是异步的,所以我需要知道,当我在Facebook上查询产品相关图片agularjs使用。

The problem is that all calls from FB API is Asynchronous, so I need to know when my query on facebook is avaliable to use in agularjs.

对于这一点,我把这个方法在我的控制器:

For this, I call this method in my controller:

Facebook.getLoginStatus();

这Facebook是我的服务,定义如下:

which Facebook is my service, defined as:

app.factory(的Facebook',函数(){

app.factory('Facebook', function() {

    getLoginStatus: function() {

        FB.getLoginStatus(function(stsResp) {
            console.log(stsResp);
            if(stsResp.authResponse) {
                // User is already logged in
                return true;
            } else {
                // User is not logged in.
                return false;
            }
        });
    }


  
  

}

}

我想在这种情况下是要检查用户是否登录。如果是真的,我会表现出一定的选择,否则,我会告诉登录按钮。

What I want in this case is to check if user is logged. If true, I'll show some options, otherwise, I'll show the Login button.

我已经尝试使用$ q.defer()函数,承诺,厂区观看响应数据,应有尽有。但任何工作,因为我想要的。我检查的基础上 Egghead.io 例子一些开发实例,但我认为我不完全理解异步调用的angularjs。

I've already try to use $q.defer() functions, promises, factorys to watch response data, everything. But anything works as I want. I've checked some development examples based on Egghead.io examples, but I think that I'm not fully understand asynchronous calls in angularjs.

先谢谢了。

推荐答案

下面是在角度的服务包Facebook的API的一个完整的基本工作的例子:

Here's a full basic working example of wrapping the Facebook API in an Angular service:

<一个href=\"http://plnkr.co/edit/0GRLdWPJOzGFY14irxLT?p=$p$pview\">http://plnkr.co/edit/0GRLdWPJOzGFY14irxLT?p=$p$pview

又见我回答这个问题(其中有角FB整合一些偏例子):

See also my answer to this question (which has some partial examples of Angular-FB integration):

<一个href=\"http://stackoverflow.com/questions/15604196/promises-in-angularjs-and-where-to-use-them/\">Promises在AngularJS何地使用它们呢?

这篇关于AngularJS - 异步调用API的Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆