2
Restangular.all('events').getList().then(function(event) {
    $scope.events = event;
});

$scope.calConfig = {
        views: [
             {"type": "agenda", "label": "Agenda"}, 
             {"type": "agendaDay", "label": "Day"}, 
             {"type": "agendaWeek", "label": "Week"}, 
             {"type": "month", "label": "Month"},
        ],
        view: "agendaWeek",
        eventSources: [$scope.events],
        now: moment().toDate(),

            /*/Callback Methods */
        },

我正在尝试使用restangular从我的MongoDB中获取数据,然后将这些数据用于全日历。问题是 $scope.events 在使用 restangular 块时未定义。

如何将它用于 $scope.calConfig?

4

2 回答 2

1

这行代码$scope.calConfig = { ... }在 Restangular 调用之前运行。

一旦 Restangular 调用完成,您需要设置配置。

Restangular.all('events').getList().then(function(event) {
    $scope.events = event;

    $scope.calConfig = {
        views: [
             {"type": "agenda", "label": "Agenda"}, 
             {"type": "agendaDay", "label": "Day"}, 
             {"type": "agendaWeek", "label": "Week"}, 
             {"type": "month", "label": "Month"},
        ],
        view: "agendaWeek",
        eventSources: [$scope.events],
        now: moment().toDate(),

            /*/Callback Methods */
    };
});

这是因为 Restangular 代码是异步的。您基本上是在要求它执行某些操作并then在完成后调用一个函数。

有关这方面的更多信息,请查看http://docs.angularjs.org/api/ng .$q


更新:这会将您的配置代码移动到它自己的函数中,并在请求返回肯定响应时调用它。您可能应该处理负面的回应。

Restangular.all('events').getList().then(function(event) {
    $scope.events = event;
    $scope.config();
});

$scope.config = function() {
    $scope.calConfig = {
        views: [
             {"type": "agenda", "label": "Agenda"}, 
             {"type": "agendaDay", "label": "Day"}, 
             {"type": "agendaWeek", "label": "Week"}, 
             {"type": "month", "label": "Month"},
        ],
        view: "agendaWeek",
        eventSources: [$scope.events],
        now: moment().toDate()
    };
}
于 2013-12-03T16:35:24.333 回答
0

restangular 调用是异步的;您需要将$scope.calConfig放入回调中,或者在执行回调时更新配置。

于 2013-12-03T16:34:23.273 回答