简体   繁体   English

$ http.get(…)。成功不是函数angular js

[英]$http.get(…).success is not a function angular js

In angular js version 1.2.9 "success" function works but in 1.6 it uses "then" function which works so how can I convert the following code using then 在angular js版本1.2.9中,“成功”功能有效,但在1.6中,它使用“ then”功能有效,因此我如何使用then转换以下代码

artistControllers.controller('DetailsController', ['$scope', '$http','$routeParams', function($scope, $http, $routeParams) {
  $http.get('js/data.json').success(function(data) {
    $scope.artists = data;
    $scope.whichItem = $routeParams.itemId;
  });
}]);

.success is deprecated for versions above 1.3. 1.3版以上的版本不推荐使用.success。 You should use .then 您应该使用.then

artistControllers.controller('DetailsController', ['$scope', '$http','$routeParams', function($scope, $http, $routeParams) {
  $http.get('js/data.json').then(function(data) {
    $scope.artists = data;
    $scope.whichItem = $routeParams.itemId;
  });
}]);

The .success syntax was correct up to Angular v1.4.3. .success语法在Angular v1.4.3之前是正确的。

For versions up to Angular v.1.6, you have to use then method. 对于Angular v.1.6以下的版本,您必须使用then方法。 The then() method takes two arguments: a success and an error callback which will be called with a response object. then()方法采用两个参数: successerror回调,将通过响应对象进行调用。

Using the then() method, attach a callback function to the returned promise . 使用then()方法,将callback函数附加到返回的promise

Something like this: 像这样:

app.controller('MainCtrl', function ($scope, $http){
   $http({
      method: 'GET',
      url: 'api/url-api'
   }).then(function (success){

   },function (error){

   });
}

See reference here. 请参阅此处的参考

Shortcut methods are also available. 也可以使用Shortcut方式。

$http.get('api/url-api').then(successCallback, errorCallback);

function successCallback(response){
    //success code
}
function errorCallback(error){
    //error code
}

The major difference between the 2 is that .then() call returns a promise (resolved with a value returned from a callback ) while .success() is more traditional way of registering callbacks and doesn't return a promise . 两者之间的主要区别是.then()调用返回一个promise (由callback返回的值解决),而.success()是注册callbacks更传统的方式,并且不返回promise

Solution

artistControllers.controller('DetailsController', ['$scope', 
  '$http','$routeParams', function($scope, $http, $routeParams) {
    $http.get('js/data.json').then(function(data) {
      $scope.artists = data;
      $scope.whichItem = $routeParams.itemId;
    });
}]);

The structure of $http has changed. $ http的结构已更改。 Use .then instead of .success 使用.then ,而不是.success

$http.get('js/data.json').then(function(data){
   console.log(data);
}).catch(function(error)){
console.log(error)
});

try this.... 尝试这个....

$http.get("http://localhost/angulartest/index.php/Items/getItems")
    .then(function (response) {

        console.log(response.data);
        $scope.records = response.data;
    });

Do three things 做三件事

  1. Replace success() with then() 用then()替换success()
  2. Replace error() with catch() 用catch()替换error()
  3. Consider response.data not the response 考虑response.data不是响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM