将服务变量值获取到控制器中

时间:2015-05-28 13:03:43

标签: angularjs

我想通过facebook在angular js app登录后获取用户名,我可以在控制台上打印这个但是在登录控制器中检索名字时有问题 这是我的登录控制器代码

/*
  Author-Himanshu
  Date- May27,2015
  Purpose- this service is responsible for facebook login
*/

'use strict'

app.factory('FacebookServices', function($http,Facebook,$rootScope){
    console.log("facebook libaray");

    return {
        facebookMe:function(){
            Facebook.api('/me', function(response) {
                $rootScope.name=response.first_name;
                $rootScope.facebookstatus=true;
                console.log($rootScope.name);
            });
            $rootScope.facebookconnection=true;
        }
    };
});

/*
FacebookLogin:function(){
    Facebook.api('/me', function(response) {
        $rootScope.$apply(function() {
            var facebookId=response.id;
            console.log(facebookId);
            $rootScope.logged=true;
            alert(facebookId);
            alert(response.email);
            $rootScope.myfacbook= response.first_name
            alert(response.first_name);
            $rootScope.UserinFacebook=true;
        });
    });
    return $rootScope.myfacbook;
} 
*/

/*
$rootScope.$apply(function() {
    var $facebookId=response;
    $rootScope.logged=true;
    $rootScope.myFb=true;
    alert(facebookId);
    alert(response.email);
    var fbname=response.first_name;
    alert(response.first_name);
    return $facebookId;
}); 
*/

这是我的服务文件

/*
 Author-Himanshu
 Date- May27,2015
 Purpose- this service is responsible for facebook login
*/
'use strict'

app.factory('FacebookServices',function($http,Facebook,$rootScope){
    console.log("facebook libaray");

    return{
        facebookMe:function(){
            Facebook.api('/me', function(response) {
                $rootScope.name=response.first_name;
                $rootScope.facebookstatus=true;
                console.log($rootScope.name);
            });
            $rootScope.facebookconnection=true;
        }
    };
});

/*
    FacebookLogin:function(){
        Facebook.api('/me', function(response) {
            $rootScope.$apply(function() {
                var facebookId=response.id;
                console.log(facebookId);
                $rootScope.logged=true;
                alert(facebookId);
                alert(response.email);
                $rootScope.myfacbook= response.first_name
                alert(response.first_name);
                $rootScope.UserinFacebook=true;
            });
        });
        return $rootScope.myfacbook;
} 
*/

/*
    $rootScope.$apply(function() {
        var $facebookId=response;
        $rootScope.logged=true;
        $rootScope.myFb=true;
        alert(facebookId);
        alert(response.email);
        var fbname=response.first_name;
        alert(response.first_name);
        return $facebookId;
    });
*/

1 个答案:

答案 0 :(得分:0)

您不应该在服务中使用$rootScope,您的服务可以帮助您解决此问题。由于角度factory是单身,因此您可以将其存储到FacebookServices函数中的变量中并稍后将其恢复。

您可以将工厂更改为以下内容:

app.factory('FacebookServices', function($http, Facebook) {
    var connectionSettings;

    function facebookMe() {
        Facebook.api('/me', function(response) {
            connectionSettings.name = response.first_name;
            connectionSettings.facebookstatus = true;
        });
        connectionSettings.facebookconnection = true;
    }

    function getName() {
        return connectionSettings.name;
    }

    return {
        facebookMe: facebookMe,
        getName: getName
    };
}

而且,虽然您没有发布您的控制器代码,但它可能是这样的:

app.controller('SomeController', function ($scope, FacebookServices) {
    $scope.facebookMe = function () {
        FacebookServices.facebookMe();
    }

    $scope.name = FacebookServices.getName();
});