我有一個模塊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