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?
这行代码$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()
};
}
restangular 调用是异步的;您需要将$scope.calConfig
放入回调中,或者在执行回调时更新配置。