首页 > 解决方案 > 添加或更新事件后,角度引导日历不会更新视图

问题描述

我正在使用 mattlewis 引导日历。事件显示正确。但是如果我将新事件添加到日历,它会复制其他日期的事件,而不是将事件添加到添加的日期。只有在刷新页面后一切正常。我也尝试了 $scope.$apply。但似乎没有任何效果。

这是我的html

<mwl-calendar events="events" view="calendarView" 
    view-date="viewDate" view-title="calendarTitle"
    on-event-click="eventClicked(calendarEvent)"
    on-event-times-changed="eventTimesChanged(calendarEvent);
    calendarEvent.startsAt = calendarNewEventStart; calendarEvent.endsAt = calendarNewEventEnd"
    cell-is-open="cellIsOpen" day-view-start="06:00"
    day-view-end="22:59" day-view-split="30"
    cell-modifier="cellModifier(calendarCell)"
    on-event-click="eventClicked(calendarEvent)"
    cell-auto-open-disabled="true"
    on-timespan-click="timespanClicked(calendarDate, calendarCell)">
</mwl-calendar>

控制器.js

$scope.events = [];
$scope.myevents = function(start, end, timezone, callback) {
    SalesNotifyService.getAllNotify().then(function(response) {
        $scope.allleadnotify  = response.data;
        console.log($scope.allleadnotify);
        var events = [];
        angular.forEach($scope.allleadnotify,function(event,key){
            var today = new Date();
            var notifyDates = new Date(event.notifyDate);
            if(event.leadName!=null){
                $scope.events.push({
                    title: event.notifyType,
                    startsAt : event.notifyDate,
                    endsAt : event.notifyEndDate,
                    withoffender: event.leadName,
                });
            } else {
                $scope.events.push({
                    title: event.notifyType,
                    startsAt : event.notifyDate,
                    endsAt : event.notifyEndDate,
                    withoffender: '',
                });
            }
        });
    });
}

现在保存后我像这样调用 $scope.myevents 函数

SalesNotifyService.saveNotify(notify).then(function(response){
    $scope.notify = response.data;
    $scope.myevents();
});

但即使我调用 myevents 函数,事件也没有正确填充到日历中。如果我已经有事件,那么在添加新事件后,旧事件正在复制和显示。如果我重新加载页面,那么一切都很好。在 html 中,我正在调用 myevents 函数在 ng-init 中。谁能告诉如何在添加事件后正确显示事件而不重新加载页面?

标签: javascriptangularjsangular-bootstrap-calendar

解决方案


也许您需要在再次添加事件之前将 $scope.events 重置为空数组?如果你不这样做,那么每次你调用 $scope.myevents() 你都会在同一个数组中再次添加事件而不删除它们。所以在 $scope.myevents 做 $scope.events=[] 在函数的开头

$scope.events = 事件;并将事件推送到本地“事件”数组中。


推荐阅读