[英]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.