2012-11-21 28 views
1

显示我有一个角的js应用。我使用GAPI登录,然后调用我的谷歌云终端获取数据。登录事情工作正常,它也从终点获取数据。AngularJS:由GAPI客户端读取的数据不会对UI

但它取出数据后,如果我的数据分配给一个变量$scope(这势必UI),它不更新UI。甚至没有错误。以下是我的代码片段。有任何想法吗?

function TestCtrl($scope){ 
    $scope.contactsList = [];//UI template is bound to this list 
//Assuming the gapi client is already loaded 
gapi.client.contactendpoint.list().execute(function(resp) { 
      if (!resp.code) { 
       $scope.contactsList = resp.items;//Does not update ui list 
      } else { 
       alert("Error, response is: " 
        + angular.toJson(resp)); 
      } 
     }); 
} 

问候, 纳迪姆乌拉

回答

3

您需要使用$适用(),如果你想更新从角框架(外例如,从浏览器的DOM事件,setTimeout的,XHR或第三范围党的图书馆)。 可在http://docs.angularjs.org/api/ng.$rootScope.Scope更多信息。

function TestCtrl($scope){ 
    $scope.contactsList = [];//UI template is bound to this list 
    //Assuming the gapi client is already loaded 
    gapi.client.contactendpoint.list().execute(function(resp) { 
    if (!resp.code) { 

     $scope.$apply(function(scope) { 
     scope.contactsList = resp.items; 
     }); 

    } else { 
     alert("Error, response is: " + angular.toJson(resp)); 
    } 
    }); 
} 
+0

非常感谢Andre Goncalves,这工作得很好:-) –

相关问题