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

Ara*_*ind 1 facebook angularjs

我无法使用角度js打印fb名称.我不确定出了什么问题.在下面的代码中,"no_name"被打印出来.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..
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

Val*_*nov 7

我假设,FB是一些外部模块,它不了解AngularJS及其事件周期(或者$scope.$apply()我认为不那么描述的调用)

在这种情况下,您应该包装回调,这会改变您的范围$apply:

 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..
        }
    });
Run Code Online (Sandbox Code Playgroud)