无法在restangular请求之外访问范围变量

时间:2014-09-30 06:27:22

标签: angularjs restangular

其余的api在restangular get请求中返回一个json对象,并且只能在函数内部访问响应

 Restangular.all('getUsers').getList().then(function(response) {
    $scope.users = response;
    angular.forEach($scope.users, function(value, key) {    //This works
        console.log(key + ': ' + value);
    });
  });

  angular.forEach($scope.users, function(value, key) {    //This does not work
        console.log(key + ': ' + value);
    });

1 个答案:

答案 0 :(得分:3)

阅读(更多)关于angularjs中的承诺。

只有在REST请求返回响应后才会执行then()内部的函数。

下面的代码在发送请求后立即运行,但肯定在处理响应之前运行。

你可以做到

$scope.$watch("users", function() {
   angular.forEach($scope.users, function(value, key) {    //This does now work
           console.log(key + ': ' + value);
       });
});
相关问题