【问题标题】:$http.get(...).success is not a function angular js$http.get(...).success 不是一个函数 angular js
【发布时间】:2017-11-08 08:49:35
【问题描述】:

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

【问题讨论】:

    标签: javascript angularjs frontend


    【解决方案1】:

    .success 不推荐用于 1.3 以上的版本。你应该使用 .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;
      });
    }]);
    

    【讨论】:

      【解决方案2】:

      .success 语法在 Angular v1.4.3 之前是正确的。

      对于 Angular v.1.6 之前的版本,您必须使用 then 方法。 then() 方法有两个参数:一个success 和一个error 回调,它们将被一个响应对象调用。

      使用then() 方法,将callback 函数附加到返回的promise

      类似这样的:

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

      参见参考here.

      Shortcut 方法也可用。

      $http.get('api/url-api').then(successCallback, errorCallback);
      
      function successCallback(response){
          //success code
      }
      function errorCallback(error){
          //error code
      }
      

      两者之间的主要区别在于.then() 调用返回一个promise(通过从callback 返回的值解析),而.success() 是注册callbacks 的更传统方式并且不返回promise

      解决方案

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

      【讨论】:

        【解决方案3】:

        试试这个....

        $http.get("http://localhost/angulartest/index.php/Items/getItems")
            .then(function (response) {
        
                console.log(response.data);
                $scope.records = response.data;
            });
        

        【讨论】:

          【解决方案4】:

          $http 的结构发生了变化。使用.then 而不是.success

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

          【讨论】:

            【解决方案5】:

            做三件事

            1. 将success()替换为then()
            2. 用 catch() 替换 error()
            3. 考虑 response.data 而不是响应

            【讨论】:

              猜你喜欢
              • 2017-05-01
              • 2016-10-02
              • 2018-02-21
              • 2016-02-05
              • 2017-12-20
              • 1970-01-01
              • 1970-01-01
              • 2020-05-01
              相关资源
              最近更新 更多