AngularJS 1.6 版本从 `.success` 方法更改为 `.then` 方法

2024-04-29

实际上我正在学习 spring 教程..来到 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 不起作用,搜索后我发现 .success 和 .error 方法已被弃用,并已从 AngularJS 1.6 中删除。我必须使用标准的 .then 方法。 有人可以将我现有的代码转换为使用 then 方法的代码吗?我尝试过,但失败了,有人可以帮忙吗?我不熟悉 javaScript 吗? 谢谢


Before

$http(...).success(function onSuccess(data, status, headers, config) {
  // Handle success
  //...
}).error(function onError(data, status, headers, config) {
  // Handle error
  //...
});

After

$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 https://docs.angularjs.org/guide/migration#migrate1.5to1.6-ng-services-%24http

也可以看看,为什么不推荐使用 Angular $http success/error 方法?从 v1.6 中删除? https://stackoverflow.com/a/35331339/5535245

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJS 1.6 版本从 `.success` 方法更改为 `.then` 方法 的相关文章

随机推荐