我有一个模块factoty做$ http调用名称:'containeData' 比我发送数据到其他工厂模块承诺 名称:'divLogicFactory'。我想发送对象数组“arraysOfDives” 到控制器。但是阵列的空间是空的。 ,因为填充数组的方法是回调。 但我不知道如何在回调完成时将对象返回给控制器。请帮助我。角厂模块得到承诺数据
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);
})]
谢谢你,谢谢你,谢谢你,谢谢你,谢谢你,谢谢你,运作良好再次感谢您 – wrcron1
@ wrcron1做接受答案..如果它有帮助。谢谢:) –
我在新的stackoverflow是我接受答案?按向上按钮后,“1”它告诉我后,15后我可以做到这一点 – wrcron1