Javascript 角度工厂模块获取承诺数据

Javascript 角度工厂模块获取承诺数据,javascript,angularjs,asynchronous,Javascript,Angularjs,Asynchronous,我有一个模块factoty,它执行$http调用名:“containeData” 然后我承诺将数据发送到其他工厂模块 名称:'divLogicFactory'。我想发送对象数组ArraySofDaves 到控制器。但这一排的房间总是空的。 因为填充数组的方法是回调。 但是我不知道在回调结束时如何将对象返回给控制器。请帮帮我 var jsonFactory = angular.module('jsonFactory',[]); jsonFactory.factory('containerDat

我有一个模块factoty,它执行$http调用名:“containeData” 然后我承诺将数据发送到其他工厂模块 名称:'divLogicFactory'。我想发送对象数组ArraySofDaves 到控制器。但这一排的房间总是空的。 因为填充数组的方法是回调。 但是我不知道在回调结束时如何将对象返回给控制器。请帮帮我

var  jsonFactory = angular.module('jsonFactory',[]);

jsonFactory.factory('containerData',function($http,$q){
    var
        defer = $q.defer(),
        data = {};

    data.getDivData = function(){

        return $http({

            method:"get",
            url:"App/metaData/divData.json"
        }).
            then(function(response){

            defer.resolve(response.data);

           // return response.data;//defer.promise;
            return defer.promise;

        },function(error){

            defer.reject(error);
            return defer.promise;

        })
    }

var divLogic = angular.module('divLogic', ['jsonFactory']);

divLogic.factory('divLogicFactory', function (containerData, $q) {



    var divLogicFactory = {};

    divLogicFactory.arraysOfDives = [];




    divLogicFactory.setDivsData = function () {

        **this is the call back method**
        containerData.getDivData().then(function (data) {

            //extract divData from call back
            for (var i = 0; i < data.length; i++) {

                var
                    height = data[i].height,
                    width = data[i].width,
                    border = data[i].border,
                    positionY = data[i].positionTopY,
                    positionX = data[i].positionLeftX,
                    templateID = data[i].templateID;


                init(height, width, border, positionY, positionX, templateID);

            }




        })
    }

    function init(height, width, border, positionY, positionX, templateID) {


        var $div = $('<div id="templateID"></div>').css({

            "height": height,
            "width": width,
            "border": border

        });


        divLogicFactory.arraysOfDives.push($div);
    }
    return divLogicFactory;
});


var controllers = angular.module('controllers', ['jsonFactory','contentItemDirective','divLogic']);

controllers.controller("HomeController", function ($http,$scope, containerData,divLogicFactory) {

    console.log(divLogicFactory);


})]
快速回答

您应该从外部返回自定义承诺对象。然后data.getDivData的函数而不是返回$http函数,但在处理$http调用时创建开销承诺被视为反模式。不要选择这个快速回答部分

代码

详细的

没有从代码的正确部分返回承诺

除此之外,您还创建了在那个地方不需要的开销承诺。当ajax调用时,$http方法确实会返回承诺对象。你可以用那种方式

data.getDivData = function(){
    return $http.get('App/metaData/divData.json').then(function(response){
        //used .then here because you could have control over a data here
        //you validate some prevalidation stuff in data, which should be there
        //before returning data. otherwise you could just return.
        //exposing only part `response.data` to consumer, other part is abstracted.
        return response.data;
    },function(error){
        return error;
    });
    //OR you could just do below as @EProgrammerNotFound suggested.
    //return $http.get('App/metaData/divData.json')
};
另外,在setDivsData方法中有DOM操作代码,但不应该在那里。如果您将该代码移动到指令中会更好

要获取使用setDivsData渲染的arraysOfDives,请使用承诺链,以便在完成div的创建后,将返回该arrayOfDives对象

工厂


谢谢,谢谢,谢谢,谢谢,谢谢,谢谢,谢谢,工作很好,谢谢,新来的stackoverflow,我接受答案了吗?在按下1上的向上按钮后,请在15分钟后告诉我我能做什么this@wrcron1请看这个@wrcron1谢谢:被否决的用户应该删除它,我认为:
data.getDivData = function(){
    return $http.get('App/metaData/divData.json').then(function(response){
        //used .then here because you could have control over a data here
        //you validate some prevalidation stuff in data, which should be there
        //before returning data. otherwise you could just return.
        //exposing only part `response.data` to consumer, other part is abstracted.
        return response.data;
    },function(error){
        return error;
    });
    //OR you could just do below as @EProgrammerNotFound suggested.
    //return $http.get('App/metaData/divData.json')
};
 divLogicFactory.setDivsData = function () {
    return containerData.getDivData().then(function (data) {
        //extract divData from call back
        for (var i = 0; i < data.length; i++) {
            var
                height = data[i].height,
                width = data[i].width,
                border = data[i].border,
                positionY = data[i].positionTopY,
                positionX = data[i].positionLeftX,
                templateID = data[i].templateID;
            init(height, width, border, positionY, positionX, templateID);
        }
        return divLogicFactory.arraysOfDives; //return arraysOfDives
 });
controllers.controller("HomeController", function ($http, $scope, containerData, divLogicFactory) {

    divLogicFactory.setDivsData(data).then(function(){
        console.log(data); //
    });

})]