javascript Angularjs/Ionic TypeError:无法读取未定义的属性“then”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28209744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:35:58  来源:igfitidea点击:

Angularjs/Ionic TypeError: Cannot read property 'then' of undefined

javascriptangularjsionic

提问by johnny Lau

codes: js:

代码: js:

angular.module('starter.services', ['ngResource'])
.factory('GetMainMenu',['$http','$q','$cacheFactory',function($http,$q,$cacheFactory) {
            var methodStr = 'JSONP';
            var urlStr = 'http://localhost/bd/wp-admin/admin-ajax.php';
            var ptStr = {action:'bd_get_main_menus',callback:'JSON_CALLBACK'};

            return {
                getMainMenuItems: function(){
                    var deferred = $q.defer();

                  $http.jsonp(urlStr,{params: ptStr})
                        .success(function (data, status) {

                            deferred.resolve(data);

                            return deferred.promise;
                        })
                        .error(function (data, status) {

                            deferred.reject(data);

                            return deferred.promise;

                        });
                }
            }

        }])

angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope, $ionicModal, $timeout, $http,GetMainMenu) {
    GetMainMenu.getMainMenuItems().then(
      function(data){
        $scope.mainMenus = data;
      });
});

run result:

运行结果:

TypeError: Cannot read property 'then' of undefined at new (ht.../www/js/controllers.js:42:33) at invoke (ht.../www/lib/ionic/js/ionic.bundle.js:11994:17)...

类型错误:在调用 (ht.../www/lib/ionic/js/ionic.bundle) 时无法读取 new (ht.../www/js/controllers.js:42:33) 处未定义的属性“then”。 js:11994:17)...

where is wrong in these codes?

这些代码哪里错了?

回答by Wayne Ellery

You need to return deferred.promisefrom the getMainMenuItems function instead of in the callback functions used for $http.jsonp. This is because getMainMenuItemsneeds to return a promise.

您需要deferred.promise从 getMainMenuItems 函数返回,而不是在用于 的回调函数中返回$http.jsonp。这是因为getMainMenuItems需要返回一个承诺。

angular.module('starter.services', ['ngResource'])
.factory('GetMainMenu',['$http','$q','$cacheFactory',function($http,$q,$cacheFactory) {
    var methodStr = 'JSONP';
    var urlStr = 'http://localhost/bd/wp-admin/admin-ajax.php';
    var ptStr = {action:'bd_get_main_menus',callback:'JSON_CALLBACK'};

    return {
        getMainMenuItems: function(){
            var deferred = $q.defer();

          $http.jsonp(urlStr,{params: ptStr})
                .success(function (data, status) {

                    deferred.resolve(data);
                })
                .error(function (data, status) {

                    deferred.reject(data);
                });

           return deferred.promise;
        }
    }
}])

Another alternative is to instead just return the promise from $http.jsonp:

另一种选择是从$http.jsonp以下位置返回承诺:

return {
        getMainMenuItems: function(){
           return $http.jsonp(urlStr,{params: ptStr});
        };

回答by hayatoShingu

You should return the promise object outside the $http. You can also return the $http because is a promise, is not necessary to have another promise.

您应该在 $http 之外返回 promise 对象。你也可以返回 $http 因为它是一个承诺,没有必要再有另一个承诺。

angular.module('starter.services', ['ngResource'])
  .factory('GetMainMenu', ['$http', '$q', '$cacheFactory', function ($http, $q, $cacheFactory) {
    var methodStr = 'JSONP';
    var urlStr = 'http://localhost/bd/wp-admin/admin-ajax.php';
    var ptStr = {action: 'bd_get_main_menus', callback: 'JSON_CALLBACK'};

    return {
      getMainMenuItems: function () {
        var deferred = $q.defer();

        $http.jsonp(urlStr, {params: ptStr})
          .success(function (data, status) {

            deferred.resolve(data);
          })
          .error(function (data, status) {

            deferred.reject(data);
          });
      return deferred.promise
      }
    }

  }])

angular.module('starter.controllers', [])
  .controller('AppCtrl', function ($scope, $ionicModal, $timeout, $http, GetMainMenu) {
    GetMainMenu.getMainMenuItems().then(
      function (data) {
        $scope.mainMenus = data;
      });
  });