Angular UI-Calendar AddEvent doesn't work when $scope.events is populated through Factory

▼魔方 西西 提交于 2019-12-11 09:54:45

问题


As shown in this plunkr example, a new event can be added and displayed when the event is appended to $scope.events directly. However, if I update $scope.events through factory.events, the new event is not displayed on the calendar.

http://plnkr.co/edit/BkdzJJ?p=preview

This has to be something really simple that I'm missing, but I've been struggling with this issue for weeks now. Can you please help?

app = angular.module('myApp');

function CalendarCtrl ($scope, gDataService) {
var today = new Date();
var m = today.getMonth();
var y = today.getFullYear();
var date = new Date();
var d = date.getDate();

$scope.events2 = [];  // This gets data through gDataService
$scope.events3 = [];  // This gets data directly from addEvent_direct method in this controller

// This is how I attempted to populate $scope.events2
$scope.$on('gDataUpdated', function(){
  //alert(gDataService.events2.length);

  $scope.events2 = gDataService.events2;
  alert('gDataService # of events =' + gDataService.events2.length.toString() 
  + '; $scope.events2 includes '+ $scope.events2.length.toString());
  // The alert message indicates $scope.events is properly updated based on 
  // gDataService.events2, but the new event doesn't appear on the calendar.
});

// This is how $scope.events3 is successfully populated
$scope.addEvent_direct = function() {

    var today = new Date();
    var m = today.getMonth();
    var y = today.getFullYear();
    var d = today.getDate();
    object = { title: 'Event directly added',
        start: new Date(y, m, 28),
        end: new Date(y, m, 29),
        className: ['openSesame']}
    $scope.events3.push(object);
}

// both events2 and events3 are included as eventSources
$scope.eventSources = [ $scope.events2 ,$scope.events3];

$scope.uiConfig = {
    fullCalendar: {
        height: 550,
        editable: false
    }
};
}
CalendarCtrl.$inject = [ '$scope','gDataService'];

// This is the factory that gets event2 data from another controller (ButtonCtrl)

app.factory("gDataService", function ($rootScope) {
var service = {};
service.events = [];
service.events2 = [];
service.added_event_short = {start: null, title: null};

service.addData = function(object) {

  this.events2.push(object);
  //alert(this.events2.length);
  $rootScope.$broadcast("gDataUpdated");
};

return service;
});

//This is a controller separated from CalendarCtrl in order to test how different controllers can share data through factory

app.controller('ButtonCtrl', function($scope,gDataService) {

  $scope.addEvent = function() {

    var today = new Date();
    var m = today.getMonth();
    var y = today.getFullYear();
    var d = today.getDate();
    object = { title: 'Open Sesame',
        start: new Date(y, m, 28),
        end: new Date(y, m, 29),
        className: ['openSesame']}

  gDataService.addData(object);
}
});

回答1:


Use this:

  //$scope.events2 = gDataService.events2; //replace with below
  angular.forEach(gDataService.events2, function(event) {
    console.log(event);
    $scope.events2.push(event);
  });

The reason is, you use '$scope.events2' to bind the data in view, but you replace the js object with new data during update. Then the watch on previous object doesn't work.



来源:https://stackoverflow.com/questions/28327100/angular-ui-calendar-addevent-doesnt-work-when-scope-events-is-populated-throug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!