2013-02-05 5 views
1

Im 각도 j를 사용하여 인쇄 된 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는 더티 체크를 할 수 있습니다. 변경되었습니다. –

관련 문제