AngularJS 1.6版将`.success`方法更改为`.then`方法
实际上,我正在学习一个春季教程。。来到他正在使用的Angularjs。成功AngularJS 1.6版将`.success`方法更改为`.then`方法,angularjs,angular-http,angularjs-1.6,Angularjs,Angular Http,Angularjs 1.6,实际上,我正在学习一个春季教程。。来到他正在使用的Angularjs。成功 var app=angular.module("MyApp",[]); .controller("MyController",function($scope,$http){ $scope.pageProduits=null; $http.get("http://localhost:8080/chercherProduits?mc") .success(function(data){
var app=angular.module("MyApp",[]);
.controller("MyController",function($scope,$http){
$scope.pageProduits=null;
$http.get("http://localhost:8080/chercherProduits?mc")
.success(function(data){
$scope.pageProduits=data;
})
.error(function(err){
console.log(err);
});
});
现在我的问题是success不起作用,经过仔细研究后,我发现.success和.error方法已被弃用,并已从AngularJS 1.6中删除。我必须遵守标准,然后改用方法。
有人能用then方法将现有代码转换成代码吗?我试过了,但失败了。有人能帮我吗?我不熟悉javaScript?
谢谢你使用
然后像这样
捕捉响应。
确保使用response.data
,因为响应数据位于data
属性下
$http.get("http://localhost:8080/chercherProduits?mc")
.then(function(response){
$scope.pageProduits=response.data;
},function(response){
console.log(response.data);
})
使用ng repeat
按行显示数据
<body ng-app="MyApp" ng-controller="MyController">
<div class="container">
<table class="table table-striped">
<thead>
<tr >
<th>ID</th>
<th>Désignation</th>
<th>Prix</th>
<th>Quantité</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="p in pageProduits">
<td>{{p.id}}</td>
<td>{{p.designation}}</td>
<td>{{p.prix}}</td>
<td>{{p.quantite}}</td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript" src="node_modules/angular/angular.min.js"></script> <script type="text/javascript" src="js/app.js"></script>
</body>
身份证件
签名
大奖赛
定量
{{p.id}}
{{p.designation}}
{{p.prix}}
{p.quantite}}
AngularJS$http
服务向服务器发出请求,并返回响应
。使用然后
而不是成功
,如下所示:
var app=angular.module("MyApp",[]);
app.controller("MyController",function($scope,$http){
$scope.pageProduits=null;
$http.get("http://localhost:8080/chercherProduits?mc")
.then(function(data){ // function that execute on ajax success
$scope.pageProduits=data.data;
}, function error(function(err){ //function that execute on ajax error
console.log(err.statusText);
});
});
根据官方文件<代码>成功和
错误
方法不再可用。这些方法被折旧,他们建议使用标准的。然后方法succes
方法只返回响应中的数据,但在中。然后方法返回完整的响应
对象
var app=angular.module("MyApp",[])
.controller("MyController",function($scope,$http){
$scope.pageProduits=null;
$http.get("http://localhost:8080/chercherProduits?mc")
.then(function(response){
if(response.status === 200) {
$scope.pageProduits=response.data
}
}, function(error) {
console.log(error)
});
});
有关更多信息:$httpBefore
之后
有关详细信息,请参阅
另请参见,thank u sachilavar app=angular.module(“MyApp”,[]);app.controller(“MyController”,函数($scope,$http){$scope.pageProduits=null;$http.get(“http://localhost:8080/chercherProduits?mc)然后(函数(resonse){$scope.pageProduits=resonse.data;},函数(resonse){console.log(resonse.data);})代码>。。。。。。。。。。IDDéSignationPrixQuantite{{p.id}{{p.designation}}{p.prix}{{p.quantite}}`但是结果没有显示:(js没有像以前那样的错误,但是我看不到像tertorial中那样的行。.你能帮我吗?是的,sachila^只是ng repeat=“p in pageProduits.content”不仅仅是ng repeat=“p in pageProduits”.bcz在该页面(json)中,页面产品“totalPages”:24,“totalElements”:120,“last”:false,“size”:5,“number”:0,“sort”:null,“numberOfElements”:5,“first”:true.“content”是需要的,Thaaaaaaaaaaankkkk Youuuuuuuuno prob.如果有帮助,请确保标记为答案。然后(响应)=>.catch(错误)=>.finally()
$http(...).success(function onSuccess(data, status, headers, config) {
// Handle success
//...
}).error(function onError(data, status, headers, config) {
// Handle error
//...
});
$http(...).then(function onSuccess(response) {
// Handle success
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
//...
}).catch(function onError(response) {
// Handle error
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
//...
});