[英]Updating nested array value when child array is updated with $promise results
我正在使用FullCalendar組件構建一個調度程序應用程序,該組件從嵌套數組模型$ scope.eventSources提供事件,該數組模型由多個事件子數組組成(gDataService.events是其中之一)。 它使用了受此plunkr示例啟發的$ resource。 http://plnkr.co/edit/pIDltQRV6TQGD4KQYnj7?p=preview 。
當以下控制器首次運行時,會將gDataService.events分配給函數(開始,結束,回調)...作為其值,並將$ scope.eventSource分配給包含1個元素的數組(此函數)。 問題是,當calFactory成功通過$ resource從服務器提取數據時,gDataService.events已正確填充了事件數據,但$ scope.eventSource沒有(即,仍保留1個功能元素的舊值)。 我嘗試了$ scope。$ apply,但它抱怨摘要已在進行中。
非常感謝您的幫助。
myApp.controller("MainCtrl", function($scope,$compile,uiCalendarConfig, calFactory, gDataService) {
var getEventSuccessCallback = function (data, status) {
gDataService.events = function(start, end, callback) {
var events;
events = data.query({
start: start,
end: end
});
events.$promise.then(function(value){
gDataService.events = value ;
callback(gDataService.events);
};
$scope.eventSources = [gDataService.events];
};
})
myApp.factory("calFactory",['$resource', function($resource) {
return {
getEvents: function () {
return $resource("/caldata/?c=to1_list",{});
}
};
}]);
原因是$scope.eventSources = [gDataService.events];
在功能內定義,應在功能外的控制器中定義。 嘗試這個:
myApp.controller("MainCtrl", function($scope,$compile,uiCalendarConfig, calFactory, gDataService) {
var getEventSuccessCallback = function (data, status) {
gDataService.events = function(start, end, callback) {
var events;
events = data.query({
start: start,
end: end
});
events.$promise.then(function(value){
gDataService.events = value ;
callback(gDataService.events);
};
};
$scope.eventSources = [gDataService.events];
});
看來您那里有一個括號問題,因為我看到您還在控制器內部定義了一個工廠。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.