【问题标题】:Angular service return undefined角度服务返回未定义
【发布时间】:2016-03-20 04:25:21
【问题描述】:

我的 Angular 服务有问题。

我的服务有下一个代码:

app.service("Utilidades", ['$http', '$window', function ($http, $window) {
return {
    Get: function (urlAbsoluta, parametros, callback) {
        var Utilidades = this;

        $http
            .get(app.UrlBase + urlAbsoluta, parametros)
            .then(function (data) {
                var Datos = angular.fromJson(data);

                Utilidades.GuardarToken(Datos.Token);

                callback(Datos);
            });
    },


    ObtenerMenu: function () {
        var Utilidades = this;

        Utilidades.Get("Administracion/Api/Usuarios/Menu", {}, function (Datos) {
            Datos = angular.fromJson(Datos.data);

            if (Datos.Error == "") {
                return Datos.Resultado;
            } else {
                return "";
            }
        });
    }
}
}]);

然后,在我的控制器中,我有下一个代码:

app.controller('LoginCtrl', ['$scope', '$http', '$location', 'Utilidades',
function Iniciador($scope, $http, $location, Utilidades) {
        var Li = this;

        Li.Usuario = "";
        Li.Contrasena = "";
        Li.Error = "";
        Li.MenuItems = [];

        Li.Menu = function () {
            Li. MenuItems = Utilidades.ObtenerMenu();
        }
    }]
);

当我运行它时,Li.MenuItems 有未定义的值,我不知道为什么。

【问题讨论】:

  • 您在服务的return {...} 之后缺少;
  • 发生这种情况是因为您正在执行的“返回”是针对“function (Datos) {”而不是针对“ObtenerMenu: function () {”。您使用 Get 函数中的回调很好地解决了这个已知问题。我不能建议任何解决方案,因为我不知道,接受不可接受的同步 ajax。
  • 是不是因为Li后面的空格。? " Li.MenuItems = Utilidades.ObtenerMenu();"
  • 你在调用 Li.Menu() 吗?
  • @BenTaliadoros 不,那个空格对代码的行为没有影响。

标签: javascript angularjs promise angular-services


【解决方案1】:

您的return 语句在您的ObtenerMenu 方法中inside 的函数中,因此ObtenerMenu 方法实际上并没有返回任何内容。您需要提供一种访问结果值的方法:

服务

app.service("Utilidades", ['$http', '$window', function ($http, $window) {
    return {
        Get: function (urlAbsoluta, parametros) {
            var Utilidades = this;

            // v------------  return statement here
            return $http
                .get(app.UrlBase + urlAbsoluta, parametros)
                .then(function (data) {
                    var Datos = angular.fromJson(data);

                    Utilidades.GuardarToken(Datos.Token);

                    // v------------  return statement here
                    return Datos;
                });
        },


        ObtenerMenu: function () {
            var Utilidades = this;

            // v------------  return statement here
            return Utilidades.Get("Administracion/Api/Usuarios/Menu", {})
                .then(function (Datos) {
                    if (Datos.Error == "") {
                        return Datos.Resultado;
                    } else {
                        return "";
                    }
                });
        }
    };
}]);

在控制器中

Li.Menu = function () {
    Utilidades.ObtenerMenu()
        .then(function (resultado) {
             Li. MenuItems = resultado;
        });
}

【讨论】:

    【解决方案2】:

    这是因为ObtenerMenu 函数是异步函数。这个函数最初不返回任何东西(所以未定义),后来,当 ajax 请求完成一段时间后,这个函数已经完成了它的执行堆栈

    【讨论】:

      猜你喜欢
      • 2019-10-09
      • 1970-01-01
      • 2018-10-04
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2016-07-28
      • 1970-01-01
      相关资源
      最近更新 更多