AngularJS 1.6版从.success方法更改为.then方法 [英] AngularJS version 1.6 change from `.success` method to `.then` method

查看:120
本文介绍了AngularJS 1.6版从.success方法更改为.then方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上是在遵循春季教程..来到Angularjs时,他正在使用.succes

actually am following a spring tutorial .. coming to Angularjs he is using .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方法。
有人可以将我现有的代码转换为当时的方法吗?我试过但失败了,谁能帮助PLZ Bcz不熟悉JavaScript?
谢谢

now my problem is that success is not working and after searshing i descovered that The .success and .error methods are deprecated and have been removed from AngularJS 1.6. i have to the standard .then method instead. Can someone convert me the existing code to code with the then mothod ? i tried but i failed can anyone helps plz bcz am not familiar with javaScript ? Thank you

推荐答案

之前



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

For more information, see AngularJS Developer Guide - Migrating from V1.5 to V1.6

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

这篇关于AngularJS 1.6版从.success方法更改为.then方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆