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获取数据,然后将数据用于fullcalendar。问题是$ scope.events在使用restangular块时是未定义的。
如何将它用于$ scope.calConfig?
答案 0 :(得分: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()
};
}
答案 1 :(得分:0)
restangular调用是异步的;您需要将$scope.calConfig
放入回调中,或者在执行回调时更新配置。