Facebook Angular Js:FB登录不更新我的$scope值

Facebook Angular Js:FB登录不更新我的$scope值,facebook,angularjs,Facebook,Angularjs,我无法使用angular js打印fb名称。我不确定出了什么问题。在下面的代码中,不会打印任何名称。JSON警报显示正确 I have this view code trying to print l from the scope <div ng-controller="TestCtrl"> {{n}} </div> This is the controller code: function TestCtrl($scope){ $scope.n="no_name";

我无法使用angular js打印fb名称。我不确定出了什么问题。在下面的代码中,不会打印任何名称。JSON警报显示正确

I have this view code trying to print l from the scope 
<div ng-controller="TestCtrl">
{{n}}
</div>

This is the controller code:
function TestCtrl($scope){

$scope.n="no_name";// default value

    FB.login(function(response) {// fb login as soon as the ui loads
        if (response.authResponse) {
            FB.api('/me', function(response) {
                alert(JSON.stringify(response));
                $scope.n=response.name;
            });
        } else {
            //do nothing..
        }
    });

}

我假设,FB是一些外部模块,它不知道AngularJS及其事件周期或调用$范围。 在这种情况下,您应该包装回调,使用$apply更改您的范围:


我假设,FB是一些外部模块,它不知道AngularJS及其事件周期或调用$范围。 在这种情况下,您应该包装回调,使用$apply更改您的范围:


你应该把标题再弄清楚一点你应该把标题再弄清楚一点才行!但为什么会这样呢?当我试图在FB.api函数中提醒$scope.n时,它会正确地提醒no_name。这意味着它可以访问正确的$scope,对吗?你可以查看本文:实际上你可以在scope循环之外读/写值,但是让AngularJS做脏检查-什么被更改了。这很有效!但为什么会这样呢?当我试图在FB.api函数中提醒$scope.n时,它会正确地提醒no_name。这意味着它可以访问正确的$scope,对吗?您可以查看本文:实际上,您可以在scope循环之外读取/写入值,但要让AngularJS执行脏检查-更改了什么。
 FB.login(function(response) {// fb login as soon as the ui loads
        if (response.authResponse) {
            FB.api('/me', function(response) {
                alert(JSON.stringify(response));
                $scope.$apply(function() {
                   $scope.n=response.name;
                });
            });
        } else {
            //do nothing..
        }
    });