3

实际上正在关注春季教程..来到Angularjs,他正在使用.succes

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 和 .error 方法已被弃用,并已从 AngularJS 1.6 中删除。我必须使用标准的 .then 方法。有人可以使用 then 方法将现有代码转换为代码吗?我试过了,但我失败了,谁能帮助不熟悉 javaScript 的 plz bcz?谢谢

4

4 回答 4

4

$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;
    //...
  });

有关更多信息,请参阅AngularJS 开发人员指南 - 从 V1.5 迁移到 V1.6

另请参阅,为什么不推荐使用角度 $http 成功/错误方法?从 v1.6 中删除?

于 2017-03-20T05:25:12.100 回答
1

像这样使用then来捕捉响应。确保使用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>
于 2017-03-20T04:08:11.500 回答
0

AngularJS$http服务向服务器发出请求,并返回一个response.Usethen而不是success如下所示:

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);
});
});
于 2017-03-20T04:11:37.050 回答
0

根据 Angular Js 官方文档。Successerror方法不再可用。这些方法已折旧,而是建议使用标准.then方法。Succces方法仅从data响应返回,但在.then方法中,返回完整response对象。

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)
    });
});

欲了解更多信息:https ://docs.angularjs.org/api/ng/service/ $http

于 2017-03-20T04:28:46.727 回答