简体   繁体   English

使用$ promise结果更新子数组时更新嵌套数组的值

[英]Updating nested array value when child array is updated with $promise results

I'm building a scheduler app using the FullCalendar component which is fed events from a nested array model $scope.eventSources consisting of multiple child arrays of events (gDataService.events being one of them). 我正在使用FullCalendar组件构建一个调度程序应用程序,该组件从嵌套数组模型$ scope.eventSources提供事件,该数组模型由多个事件子数组组成(gDataService.events是其中之一)。 It uses $resource inspired by this plunkr example. 它使用了受此plunkr示例启发的$ resource。 http://plnkr.co/edit/pIDltQRV6TQGD4KQYnj7?p=preview . http://plnkr.co/edit/pIDltQRV6TQGD4KQYnj7?p=preview

When the following controller first runs, gDataService.events is assigned the function(start, end, callback)... as its value, and $scope.eventSource is assigned an array of 1 element (which is the function). 当以下控制器首次运行时,会将gDataService.events分配给函数(开始,结束,回调)...作为其值,并将$ scope.eventSource分配给包含1个元素的数组(此函数)。 The problem is when the calFactory successfully pulls the data from the server through $resource, gDataService.events is properly populated with event data, but $scope.eventSource doesn't (ie. still keeps the old value of 1 function element). 问题是,当calFactory成功通过$ resource从服务器提取数据时,gDataService.events已正确填充了事件数据,但$ scope.eventSource没有(即,仍保留1个功能元素的旧值)。 I tried $scope.$apply, but it complains that digest is already in progress. 我尝试了$ scope。$ apply,但它抱怨摘要已在进行中。

Your help is much appreciated. 非常感谢您的帮助。

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",{});
    }
};
}]);

The reason is that $scope.eventSources = [gDataService.events]; 原因是$scope.eventSources = [gDataService.events]; is defined within the function and should be defined in the controller, outside of the function. 在功能内定义,应在功能外的控制器中定义。 Try this: 尝试这个:

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];
});

It seems you have a bracket issue there, since I see you also define a factory inside a controller. 看来您那里有一个括号问题,因为我看到您还在控制器内部定义了一个工厂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM