Javascript 从AngularJS中的回调函数中访问父作用域中的变量
我有一段代码,在AngularJS中是这样的Javascript 从AngularJS中的回调函数中访问父作用域中的变量,javascript,angularjs,Javascript,Angularjs,我有一段代码,在AngularJS中是这样的 $scope.loadItems = function($query){ $http({method: 'GET', url: '/query=' + $query}).then( function($response){ $scope.result = $response['data']; }, function($response){$scope.result = [];})
$scope.loadItems = function($query){
$http({method: 'GET', url: '/query=' + $query}).then(
function($response){
$scope.result = $response['data'];
}, function($response){$scope.result = [];});
return $scope.result;
}
$scope.loadItems需要分配一个数组变量。我是通过在Ajax调用的一个回调函数中进行Ajax调用来获取这个数组的。但是,我不得不将我获得的数组设置为$scope变量的属性,以使它在最外层函数的作用域中可访问,我不喜欢它,因为它感觉不正确(我确信我将在某个地方开始使用$scope.result,而不会过多考虑它,最终会得到一些非常复杂的结果)。有什么方法可以将从成功回调函数获得的数组传递给最外层的函数吗?您是否尝试过:
$scope.loadItems=函数($query){
$scope.result=$http({method'GET',url:'/query='+$query})。然后(
功能(响应){
返回$response['data'];
},函数($response){return[];});
返回$scope.result;
}
如果这不起作用,请在评论中告诉我,我将删除此答案