javascript - Angularjs/ ionic 类型错误 : Cannot read property 'then' of undefined

标签 javascript angularjs ionic-framework

代码: 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;
      });
});

运行结果:

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

这些代码哪里错了?

最佳答案

您需要从 getMainMenuItems 函数返回 deferred.promise,而不是在用于 $http.jsonp 的回调函数中返回。这是因为 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;
        }
    }
}])

另一种选择是从 $http.jsonp 返回 promise :

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

关于javascript - Angularjs/ ionic 类型错误 : Cannot read property 'then' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28209744/

相关文章:

javascript - $ ("#id").click(函数(){计算()});

javascript - 无法引用jquery

javascript - 2 个不同的 Controller : hiding elements on the action of one of them

javascript - 重新加载 angularjs 中的选项卡

android - net::ERR_CONNECTION_REFUSED ionic

node.js - 在Windows 8.1上安装node.js时出错

javascript - 奇怪的node.js运行时错误: XXX is not a function (But I indeed define XXX as a function~!)

javascript - 将模板包含到特定 div

javascript - 如何在 Directive Link 函数中为变量添加 TemplateURL?

javascript - Angular Dir-Pagination 并调用 Controller 功能