【问题标题】:AngularJS wait for $http responseAngularJS 等待 $http 响应
【发布时间】:2015-07-16 10:00:49
【问题描述】:

我需要等到收到来自 $http 获取请求的响应。这是我的代码:

var app = angular.module('prova', ['ui.bootstrap']);

app.controller('MainCtrl', function($scope, $http) {

$scope.currentPage = 1;
$scope.itemsPerPage = 5;
$scope.maxSize = 5;
$scope.tickets = [];

$http({method: 'GET', url: 'getNews.json'}).
success(function(data, status, headers, config) {

    $scope.tickets = data;
}).
error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

for (var i = 0; i < 78; i++) {
    $scope.tickets.push('ticket ' + i);
}
$scope.noOfPages = $scope.tickets.length / $scope.itemsPerPage;

$scope.$watch('currentPage', function() {
    var begin = ($scope.currentPage - 1) * $scope.itemsPerPage;
    var end = begin + $scope.itemsPerPage;

    $scope.paged = {
        tickets: $scope.tickets.slice(begin, end)
    }
   });
});

查看:

<!DOCTYPE html>
<html ng-app="plunker">

<head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <link data-require="bootstrap-css@3.0.3" data-semver="3.0.3" rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
    <script data-require="angular.js@*" data-semver="1.2.12" src="http://code.angularjs.org/1.2.12/angular.js"></script>
    <script data-require="angular-ui-bootstrap@0.11.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.min.js"></script>
    <script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script data-require="bootstrap@3.0.2" data-semver="3.0.2" src="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
    <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">

    <div>{{noOfPages}} &nbsp; {{currentPage}} &nbsp; {{maxSize}}
        <pagination ng-model="currentPage" total-items="tickets.length" items-per-page="5"></pagination>
    </div>
    <div class="alert alert-info" ng-repeat="tic in paged.tickets">
        {{tic}}
    </div>

</body>

</html>

【问题讨论】:

  • 只需将需要的代码放入成功处理程序中
  • 您是否在视图中使用 ng-repeat?如果是,它会在 $scope.tickets 收到数据时自动处理填充。为了使您的分页工作,请改用过滤器。那么你就不需要额外的手表了。检查我的答案here
  • @jme11 感谢您的回答..我会检查一下,同时我用我的观点编辑了我的问题..
  • @jme11 无论如何,问题是在某种程度上我必须等待 AJAX 调用的响应..
  • 正如@RoshanMathews 在他的回答中所说,只需将需要等待响应的代码放在您 $http 调用的 .success 函数中即可。

标签: javascript angularjs httprequest


【解决方案1】:

Angular 的$http 将返回一个承诺,您可以将代码添加到success 方法中。要重用$http documentation 中的示例:

// Simple GET request example :
$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

编辑:

如下代码:

for (var i = 0; i < 78; i++) {
    $scope.tickets.push('ticket ' + i);
}
$scope.noOfPages = $scope.tickets.length / $scope.itemsPerPage;

$scope.$watch('currentPage', function() {
    var begin = ($scope.currentPage - 1) * $scope.itemsPerPage;
    var end = begin + $scope.itemsPerPage;

    $scope.paged = {
        tickets: $scope.tickets.slice(begin, end)
    }
});

应该移到.success 块中。希望对您有所帮助。

【讨论】:

  • 它不起作用..这段代码和我的一样,它不等待响应..
猜你喜欢
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 2015-12-28
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
相关资源
最近更新 更多