2013-02-05 26 views
1

我無法使用角度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.. 
     } 
    }); 

} 
+0

你應該澄清的是工作的標題多一點點 – nick

回答

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.. 
     } 
    }); 
+0

!但爲什麼這樣呢?當我嘗試在FB.api函數內提醒$ scope.n時,它會正確地提醒no_name。這意味着它有權訪問正確的$範圍,對吧? – Aravind

+0

但無論如何,非常感謝!解決了我的問題! :) – Aravind

+0

你可以查看這篇文章:http://onehungrymind.com/notes-on-angularjs-scope-life-cycle/其實你可以在範圍週期之外讀/寫值,但是讓AngularJS做髒檢查 - 什麼是改變。 –