最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularjsIonic TypeError: Cannot read property 'then' of undefined - Stack Overflow

programmeradmin4浏览0评论

codes: 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)...

where is wrong in these codes?

codes: 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)...

where is wrong in these codes?

Share Improve this question edited Jan 29, 2015 at 8:14 johnny Lau asked Jan 29, 2015 at 8:07 johnny Laujohnny Lau 691 gold badge1 silver badge4 bronze badges 2
  • "Cannot read property 'then' of undefined" Your GetMainMenu.getMainMenuItems() is undefined, and it supposed to be promise object to call method then(). Format your code. – Mike Commented Jan 29, 2015 at 8:11
  • Don't use the deferred antipattern! – Bergi Commented May 2, 2015 at 18:35
Add a ment  | 

2 Answers 2

Reset to default 9

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

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:

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

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.

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;
      });
  });
发布评论

评论列表(0)

  1. 暂无评论