gpt4 book ai didi

javascript - AngularJS 等待 $http 响应

转载 作者:行者123 更新时间:2023-11-28 00:04:46 26 4
gpt4 key购买 nike

我需要等待 $http get 请求的响应。这是我的代码:

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>

最佳答案

Angular 的 $http 将返回一个 Promise,您可以将代码添加到 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 block 中。希望有帮助。

关于javascript - AngularJS 等待 $http 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31451098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com