简体   繁体   English

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

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

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)... TypeError:无法在调用时读取未定义的属性'then'(ht ... / www / js / controllers.js:42:33)(ht ... / www / lib / ionic / js / ionic.bundle。 JS:11994:17)...

where is wrong in these codes? 这些代码哪里错了?

You need to return deferred.promise from the getMainMenuItems function instead of in the callback functions used for $http.jsonp . 您需要从getMainMenuItems函数而不是在用于$http.jsonp的回调函数中返回deferred.promise This is because getMainMenuItems needs to return a promise. 这是因为getMainMenuItems需要返回一个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 : 另一种选择是从$http.jsonp返回承诺:

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

You should return the promise object outside the $http. 你应该在$ http之外返回promise对象。 You can also return the $http because is a promise, is not necessary to have another 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;
      });
  });

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

相关问题 使用 AngularJS 和 Ionic 出现错误“TypeError:无法读取未定义的属性‘email’” - Error "TypeError: Cannot read property 'email' of undefined" using AngularJS and Ionic TypeError:无法使用AngularJs读取IONIC中未定义的属性'googleplus' - TypeError: Cannot read property 'googleplus' of undefined in IONIC with AngularJs Ionic:TypeError:无法读取未定义的属性“ ready” - Ionic: TypeError: Cannot read property 'ready' of undefined 无法读取 AngularJS Ionic 中未定义的属性数据传输 - Cannot read property dataTransfer of undefined in AngularJS Ionic 类型错误:无法使用 AngularJS 读取未定义的属性“推送” - TypeError: Cannot read property 'push' of undefined with AngularJS AngularJS:TypeError:无法读取未定义的属性“childNodes” - AngularJS : TypeError: Cannot read property 'childNodes' of undefined angularJS)TypeError:无法读取未定义的属性“ push” - angularJS) TypeError: Cannot read property 'push' of undefined TypeError:无法读取angularjs中未定义的属性“ reduce” - TypeError: Cannot read property 'reduce' of undefined in angularjs AngularJS - TypeError:无法读取未定义的属性'go' - AngularJS - TypeError: Cannot read property 'go' of undefined TypeError:无法读取AngularJS上未定义的属性“ get” - TypeError: Cannot read property 'get' of undefined on AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM