数据不充当服务的全局表单工厂

Data not acting as global form factory to services

本文关键字:全局 表单 工厂 服务 数据      更新时间:2023-09-26

下面我从 API 获取信息。 在控制台中,当我返回无法进入服务的 iteminfo 数据时,我获得了数据。

这是我的工厂代码

factmodule.factory("OrderFactory", function() {
    var iteminfo;
    var Iteminforesource = $resource("http://demo.foodzard.in/api/menu");
    return {
        Iteminforesource.get(function(data) {
            iteminfo = data.message;
            console.log("this is menu " + iteminfo)
        }, function(d, s) {
            console.log("Error*** " + s)
        })
        return iteminfo;
    }
})

这是我的服务代码。在控制台中数据显示未定义

servctrl.service("OrderService", function(OrderFactory) {
    this.getAllInfoItem = function() {
        return OrderFactory.Iteminforesource();
        console.log(OrderFactory.Iteminforesource());
    }
})

请帮助我

您可以在此处阅读有关promise对象的信息,也可以在此处阅读特定于 Angular 的信息。

factmodule.factory("OrderFactory", function() {
    var Iteminforesource = $resource("http://demo.foodzard.in/api/menu");
    return {
        // return promise object
        iteminfo:function{
        return Iteminforesource.get().$promise.then(function(data) {
            // promise will resolved with data.message
            return data.message;
        });
    }
    }
})
servctrl.service("OrderService", function(OrderFactory) {
    this.getAllInfoItem = function() {
        // this service also will return promise
        return OrderFactory.iteminfo();
    }
})
// in controllers getAllInfoItem must be used like this
orderService.getAllInfoItem().then(function(message) {
    console.log(message);
})